Edit report at http://bugs.php.net/bug.php?id=55051&edit=1

 ID:                 55051
 Updated by:         ras...@php.net
 Reported by:        kloas at mail dot ru
 Summary:            bag with function str_replace
-Status:             Open
+Status:             Bogus
 Type:               Feature/Change Request
 Package:            PHP options/info functions
 Operating System:   win 7
 PHP Version:        5.3.6
 Block user comment: N
 Private report:     N

 New Comment:

As the docs state, if the search and replace params are arrays, their elements 

are processed first to last. That means it iterates over the string and does 

each replacement from the arrays one by one, left to right. That means you have 

these steps:



1234567890 Original string

9234567890 Replace all 1's with 9's

9834567890 Replace all 2's with 8's

9874567890 Replace all 3's with 7's

9876567890 Replace all 4's with 6's

9876567890 Replace all 5's with 5's

9874547890 Replace all 6's with 4's

9834543890 Replace all 7's with 3's

9234543290 Replace all 8's with 2's

1234543210 Replace all 9's with 1's



If you want to translate chars in a single pass, use strtr()



eg. echo strtr("1234567890","9876543210",$str);


Previous Comments:
------------------------------------------------------------------------
[2011-06-14 00:20:49] kloas at mail dot ru

Description:
------------
hi? im from russian? english is bad

so here code with bug



<?

$a=array("1","2","3","4","5","6","7","8","9","0");



$b=array("9","8","7","6","5","4","3","2","1","0");



$str="1234567890";



echo str_replace($a,$b,$str);



?>

////echo 1234543210    





Test script:
---------------
<?

$a=array("1","2","3","4","5","6","7","8","9","0");



$b=array("9","8","7","6","5","4","3","2","1","0");



$str="1234567890";



echo str_replace($a,$b,$str);



?>



must be  0987654321   

Expected result:
----------------
<?

$a=array("1","2","3","4","5","6","7","8","9","0");



$b=array("9","8","7","6","5","4","3","2","1","0");



$str="1234567890";



echo str_replace($a,$b,$str);



?>

Actual result:
--------------
<?

$a=array("1","2","3","4","5","6","7","8","9","0");



$b=array("9","8","7","6","5","4","3","2","1","0");



$str="1234567890";



echo str_replace($a,$b,$str);



?>


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=55051&edit=1

Reply via email to