Multiple replace within text

I’m looking to find and replace multiple strings. The string pattern is allways the same, except the middle part which I used to dtermine what to replace it with.

eg: <!--FIND123FIND-->

I want to loop through each one and replace with something I determine.

How can this be achieved with preg_match_all?

View source in your browser,
Two examples showing where you can put delimiters and modifiers (s=treat as single line U=ungreedy play around by removing it to see the results).

$str = '<!--FIND123FIND-->

$pattern = '~<!--(.*)-->~sU';
$pattern2 = '<!--(.*)-->';

$replace = '<!--hello-->';

$out = preg_replace($pattern, $replace, $str);

echo $str."\n";
echo $out."\n";

preg_match_all("~$pattern2~sU", $str, $out2); 

echo'<pre>'; print_r($out2); echo '</pre>';

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.