Jump to content

string replacement - need help


shishisu

Recommended Posts

Hi guys, I need some help here... Any input is greatly appreciated:

 

I have a long string of HTML code, looks kind of like this:

 

this is a test link to <!--source--><a href="www.apple.com">Apple</a><!--replace--><!--<a href="www.mozilla.org">Mozilla</a>--><!--end-->

I would like to use a PHP script to find all the occurrence of <!--source--> and replace everything up to <!--replace--> with the text before <!--end-->

 

The result string should look like:

this is a test link to <a href="www.mozilla.org">Mozilla</a>

Link to comment
Share on other sites

<?php

$string = <<<STR
this is a test link to
<!--source--><a href="www.apple.com">Apple</a>
<!--replace--><!--<a href="www.mozilla.org">Mozilla</a>--><!--end-->
STR;

print $string = preg_replace('/<!--source-->.+?<!--replace--><!--(.+?)--><!--end-->/s', '\1', $string);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.