Forum Moderators: coopster

Message Too Old, No Replies

php preg replace array

         

jman11

6:26 am on Nov 10, 2009 (gmt 0)

10+ Year Member



how do you replace a value with an array of values several times?

JohnCanyon

4:49 pm on Nov 10, 2009 (gmt 0)

10+ Year Member



$find_array = array('foo','bar');
$replace_array = array('moo','car');

$string = 'foo bar';
echo str_replace($find_array, $replace_array, $string);

//outputs
moo car

jman11

9:47 pm on Nov 10, 2009 (gmt 0)

10+ Year Member



everyone does replace with both find an array and the replace an array. how do you do it with just the replace an array?

JohnCanyon

10:42 pm on Nov 10, 2009 (gmt 0)

10+ Year Member



Because, the occurance of the first element in the replace array would overwrite all occurances of the find making the additional replaces irrelevant.