str_replace weird output

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello guys :-)

I have a question. I have been using str_replace() quite a lot of times, 
and never encountered any issue. But this time, something weird happened, 
and I am wondering wether, in fact, I have never really understood how 
this function worked, or if there is some kind of bug.

If I do that :

<?php
$test = 'bla';
$un = array ('b', 'l', 'a');
$deux = array ('c', 'd', 'e');
$test = str_replace ($un, $deux, $test);
echo $test , '<br />';
?>

No prob, the output is the one I would have expected : cde

But if I do that :

<?php
$texte = 'cd' ;
$original = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
$modif = array ('c', 'd', 'e', 'f', 'g', 'h', 'i');
$texte = str_replace($original, $modif, $texte) ;
echo $texte, ' <br />' ;
?>

The result is : ih

Why ? 
It seems to me that str_replace is messing up a litlle bit if there are 
identical values in both replacement and search arrays.
Or am I missing something ?

Thanks a lot :-)

Johan

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux