Why Str_replace returned data errors

Source: Internet
Author: User
Example: Replace comma, space character, or Chinese comma
$str = ' correct, contact us goodsjob,goodsjob ';
$reg = Array (', ', ' ', ', ');
$strs = ' username '. Str_replace ($reg, '%, or username like% ', $str). ' %';
The returned data is: username like% correct%,%, or username like%or%, or username like%username%, or username like%like%, or username like Percent% contact us%, or username like%goodsjob%, or username like%goodsjob%, for what?

The correct result should be: Usernme like% correct%, or username like% contact us%, or username like%goodsjob%

Reply content:

Example: Replace comma, space character, or Chinese comma
$str = ' correct, contact us goodsjob,goodsjob ';
$reg = Array (', ', ' ', ', ');
$strs = ' username '. Str_replace ($reg, '%, or username like% ', $str). ' %';
The returned data is: username like% correct%,%, or username like%or%, or username like%username%, or username like%like%, or username like Percent% contact us%, or username like%goodsjob%, or username like%goodsjob%, for what?

The correct result should be: Usernme like% correct%, or username like% contact us%, or username like%goodsjob%

Replace the space with other characters $str in the process of substitution, = "%, or username like%, there is also a string that satisfies your substitution rules so it will affect the result

        $str = '正确,联系我们 goodsjob,goodsjob';        $str = str_replace(' ', ' ', $str);        $reg = array(',',','," ");        $strs = 'username like %'.str_replace($reg,'%, or username like %',$str).'%';                

注意:Because the Str_replace () function replaces left-to-right, it may replace the previously inserted value with multiple substitutions.

  • Related Article

    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.