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
https://forums.phpfreaks.com/topic/57185-string-replacement-need-help/
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);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.