Replace using arrays...
Posted: Sat Jan 22, 2005 11:15 pm
OK I admit that I am new to PHP, and that the amount of commands that can manipulate arrays are overwhelming to say the least...But I'm having trouble doing something, which is not really covered by the PHP documentation (I think):
Performing regular expression matches in string of text (HTML), where each match is replaced by the value of an array.
In code it would read something like (I did not test the code, it's just there for illustration purposes
Now here's the problem. What function is most useful when I want to replace matches *one at a time* so the resulting string would read:
blablablablablatext1blatext2blablatext3 <- results from array
instead of
blablablablablatext1blatext1blablatext1 <- the first match all the way
I've read about preg_replace_callback, but I can't get the array fed into the callback. Which is the solution I guess...
Is there a way, not to complicated,...
Performing regular expression matches in string of text (HTML), where each match is replaced by the value of an array.
In code it would read something like (I did not test the code, it's just there for illustration purposes
Code: Select all
$my_arr=array('text1','text2,'text3');
$html_string="blablablablabla_blah_bla_blah_blabla_blah_";
//match against "_blah_" not "bla"
$reg_exp="/_blah_/";blablablablablatext1blatext2blablatext3 <- results from array
instead of
blablablablablatext1blatext1blablatext1 <- the first match all the way
I've read about preg_replace_callback, but I can't get the array fed into the callback. Which is the solution I guess...
Is there a way, not to complicated,...