It's weird to replace an array with Str_replace.

Source: Internet
Author: User
$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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.