It is a bit strange to replace arrays with str_replace $ str & nbsp; 2abc; $ arr1 & nbsp; array (0, & nbsp; 1, & nbsp; 2, & nbsp; 3, & nbsp; 4, & nbsp; 5, & nbsp; 6); $ arr2 & nbsp; array (9, & nbsp; 8, & nbsp; it's a bit strange to replace arrays with str_replace
$ Str = '2abc ';
$ Arr1 = array ('0', '1', '2', '3', '4', '5', '6 ');
$ Arr2 = array ('9', '8', '7', '6', '5', '4', '3 ');
$ Str = str_replace ($ arr1, $ arr2, $ str );
Echo $ str;
// The output result is normal: 7abc
$ Str = '2abc ';
$ Arr1 = array ('0', '1', '2', '3', '4', '5', '6', '7 ');
$ Arr2 = array ('9', '8', '7', '6', '5', '4', '3', 'w ');
$ Str = str_replace ($ arr1, $ arr2, $ str );
Echo $ str;
// The output result is incorrect: Wabc
Why?
Thank you!
Share:
------ Solution --------------------
'7'
'W'
Isn't there this behind? the first time is 2-> 7, the second time is 7-> w
------ Solution --------------------
$ Str = '2abc ';
$ Arr1 = array ('0', '1', '2', '3', '4', '5', '6', '7 ');
$ Arr2 = array ('9', '8', '7', '6', '5', '4', '3', 'w ');
$ Str = str_replace ($ arr1, $ arr2, $ str );
Echo $ str;
// The output result is incorrect: Wabc
He means that when $ arr1 is searched for the first time, 2 is replaced with 7, so $ str = 7abc and then $ arr1 is searched for again, 7 is replaced with W, so it becomes Wabc.