PHP Regular Replacement preg_replace_php tutorial

Source: Internet
Author: User
There is a question of how to replace a particular string in a string. Example: Replace the string: ABC Fdab ertde FDA ABC FDD in ABC, where ABC is case-insensitive and replaced with the following string: Fdab ertde FDA FDD

At that time, I thought of two strategies preg_replace regular replacement, Preg_split split merge. Ah ah, but there is no manual at the hand, always have no courage to try, here show the code bar, it seems to be careful later. On the Code bar:


[PHP]
$STR = ' ABC Fdab ertde FDA abc FDD ';
$pat = '/abc/i ';
$rtn = Preg_replace ($pat, ", $str,-1);
Echo ' orig: ', $str, '
';
Echo ' dest: ', $rtn;

$STR = ' ABC Fdab ertde FDA abc FDD ';
$pat = '/abc/i ';
$rtn = Preg_replace ($pat, ", $str,-1);
Echo ' orig: ', $str, '
';
Echo ' dest: ', $rtn; Please click Preg_replace to see the usage of the function. Showshowpreg_split It again:


[PHP]
$STR = ' ABC Fdab ertde FDA abc FDD ';
$pat = '/abc/i ';
$arr = Preg_split ($pat, $STR);
$rtn = Implode (' ', $arr);
Echo ' orig: ', $str, '
';
Echo ' dest: ', $rtn;

$STR = ' ABC Fdab ertde FDA abc FDD ';
$pat = '/abc/i ';
$arr = Preg_split ($pat, $STR);
$rtn = Implode (' ', $arr);
Echo ' orig: ', $str, '
';
Echo ' dest: ', $rtn;
Hehe, although this way is very stupid, but it is a way ah, but in the final analysis is the wording of the regular. Is there another way, thanks to manual in hand, Kung Fu, there is another kind of stupid method immediately:


[Plain]
$STR = ' ABC Fdab ertde FDA abc FDD ';
$tran = Array (' abc ' = = ', ' abc ' + = ');
$rtn = Strtr ($str, $tran);
Echo ' orig: ', $str, '
';
Echo ' dest: ', $rtn;

$STR = ' ABC Fdab ertde FDA abc FDD ';
$tran = Array (' abc ' = = ', ' abc ' + = ');
$rtn = Strtr ($str, $tran);
Echo ' orig: ', $str, '
';
Echo ' dest: ', $rtn;

This way some trickery, mainly use STRTR to evade the regular, deviate from others ' purpose, also is not a good method!

This time, the question of the regular is to reflect, although the regular learning a lot, also wrote some. Can always use the time to read the manual, constantly retry rewriting, not clear on the chest. Heart to it some timid, should later write more practice, truly familiar with this basic skills.

http://www.bkjia.com/PHPjc/477587.html www.bkjia.com true http://www.bkjia.com/PHPjc/477587.html techarticle There is a question of how to replace a particular string in a string. Example: Replace the string: ABC Fdab ertde FDA ABC FDD in ABC, where ABC is case-insensitive, replaced ...

  • 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.