lundi 23 février 2015

str_replace Replacing same occurence of string as many times as it founds it

I am trying to replace some strings from an array with other strings, just that if the PHP intepretor finds the same string again it will apply the values to replace as tp the same string as many times the string is in the array. For example:



$html = 'first
first
second
third
third';
$array = array('first', 'first', 'second', 'third', 'third');
foreach ($array as $elem) {
$html = str_replace($elem, $elem.' | added', $html);
}
var_dump($html); //will result

string 'first | added | added

first | added | added

second | added

third | added | added

third | added | added' (length=158)


EXPECTED OUTPUT



string 'first | added

first | added

second | added

third | added

third | added' (length=158)

Aucun commentaire:

Enregistrer un commentaire