It's a bit strange to replace an array 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!
Reply to discussion (solution)
'7'
'W'
Isn't there this behind? the first time is 2-> 7, the second time is 7-> w
'7'
'W'
Isn't there this behind? the first time is 2-> 7, the second time is 7-> w
These two codes are tested separately. they are not in the same file.
I know, I am talking about the second one.
$ 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.