Multiple preg_match_all on same line
Posted: Thu Apr 17, 2008 11:28 am
Hello All,
I am trying to use regex to replace multiple values in a string, which all use the same de-limiter.
I have the following small function to do this:
If I pass the following string into that function:
"some text #replace 1# and some more text"
Then it works fine, however, if I try and pass
"some text #replace 1# and some more text #replace 2# and even more text"
then the function fails, because, instead of evaluating #replace 1# and then evaluating #replace 2#
it actually evaluates:
#replace 1# and some more text #replace 2#
i.e. it is evaluating on the two outermost "#" characters.
Is it possible to get it to evaluate the first pair of # characters, then the second pair and so on?
I have read the documentation and just cannot figure it, if anyone can help me out I would be very grateful!
Thanks!
I am trying to use regex to replace multiple values in a string, which all use the same de-limiter.
I have the following small function to do this:
Code: Select all
function embeded($retval){
while (preg_match_all("|#(.*)#|", $retval, $out)){
$retval = preg_replace("|#(.*)#|", "replacement text", $retval);
}
return $retval;
}"some text #replace 1# and some more text"
Then it works fine, however, if I try and pass
"some text #replace 1# and some more text #replace 2# and even more text"
then the function fails, because, instead of evaluating #replace 1# and then evaluating #replace 2#
it actually evaluates:
#replace 1# and some more text #replace 2#
i.e. it is evaluating on the two outermost "#" characters.
Is it possible to get it to evaluate the first pair of # characters, then the second pair and so on?
I have read the documentation and just cannot figure it, if anyone can help me out I would be very grateful!
Thanks!