$str = ' 2abc '; $arr 1 = Array (' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 '), $arr 2 = Array (' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 '); $str = str _replace ($arr 1, $arr 2, $STR); echo $str;//output normal: 7ABC
$str = ' 2abc '; $arr 1 = Array (' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 '), $arr 2 = Array (' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' W '); $str = Str_replace ($arr 1, $arr 2, $STR); echo $str;//Output error: WABC
Excuse me, why is this so?
Thank you!
Reply to discussion (solution)
' 7 '
' W '
Isn't there this one back there, first time 2->7, second time 7->w
' 7 '
' W '
Isn't there this one back there, first time 2->7, second time 7->w
is to test the above two pieces of code separately, and they are not in the same file.
I know, I'm talking about the second one.
$str = ' 2abc ';
$arr 1 = Array (' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ');
$arr 2 = Array (' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' W ');
$str = Str_replace ($arr 1, $arr 2, $STR);
Echo $str;
Incorrect output: WABC
When he first searched for $ARR1, he replaced 2 with 7 so $STR=7ABC then went on to find $ARR1 and replaced the 7 with W so it became WABC.