PHP string substitution related problems, found how can not be implemented, please expert pointing

Source: Internet
Author: User
PHP string substitution problem, found how can not be achieved, please expert pointing

There is a requirement: there is a string ' abc ' that wants to replace the ' a ' in it with ' AB ', where ' B ' is replaced by ' CD ', that is, the last to get ' ABCDC '.

Find out how can not be achieved, such as with ' Str_replace '

$STR = ' abc ';
Var_dump (Str_replace ([' A ', ' B '], [' AB ', ' CD '], $STR));

The result returned is ' ACDCDC '. The ' str_replace ' process seems to be to replace ' a ' with ' ab ' to get ' abbc ', and then replace ' ABBC ' with ' B ' in ' CD ' to get ' ACDCDC ', the whole process is: ' abc ', ' Abbc ', ' ACDCDC '. ' Preg_replace ' is the same process.

Excuse me, how to realize ' abc ', ' ABCDC '???

Wait for the master, thank you very much
------to solve the idea----------------------
Do not hang in a tree, PHP has provided a variety of processing methods
$STR = ' abc ';
Echo strtr ($str, Array (' a ' = = ' ab ', ' b ' = ' CD '));
Abcdc
  • 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.