Global preg_replace?

Hi Sitepoint!

I want to cut out everything from $a to $b in a text variable, eg.
$a=“start”;
$b=“end”;
$text=“some text start text in between end text later on”;

so $text would be “some text text later on”

is there a way to do this in php? I know how to do it in perl but php doesnt seem to have a global flag…

Try str_replace(). If you want to use regex though, check out [URL=http://www.php.net/preg_replace]preg_replace() and [URL=http://www.php.net/preg_match_all]preg_match_all().

Good luck!

can I do that with str_replace? it doesnt look like it allows that wildcard-style matching.


$string = preg_replace('/\\bstart\\b.*\\bend\\b/', '', $string);

[fphp]preg_replace[/fphp] is global unless you specify the $limit parameter.