PHP regular expression replacement preg_replace

Source: Internet
Author: User
Tags php regular expression
There is a problem: how to replace a specific string in a string. For example, replace abc in the string: abcfdabertDefdaAbcfdd, where abc is case-insensitive. after the replacement, the string is: fdabertDefdafdd, which immediately comes up with two policies: preg_r... "/> <scripttype =" text/javascript

There is a problem: how to replace a specific string in a string. For example, replace abc in the string Abc fdab ertDe fda abc fdd, where abc is case-insensitive and the string after replacement is fdab ertDe fda fdd.

At that time, we immediately thought of two policies: preg_replace regular expression replacement and preg_split split and merge. Alas, but there was no manual at the time, and there was always no courage to try it. show the code here. it seems that you have to pay attention to it in the future. 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; click preg_replace to View function usage. Then showshowpreg_split:


[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;
Haha, this method is stupid, but it is also a way, but it is still a regular expression in the final analysis. Is there another way? thanks to the manual in hand, there is no way to be stupid:


[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 method is tricky. it is not a good method to avoid regular expressions and deviate from others' purposes!

This time, we should reflect on the regular expressions. although I have learned a lot about the regular expressions, I have also written some. You can always read the manual when using it, and rewrite the manual without stopping it. I am afraid of it. I should write more and practice more in the future to become familiar with this basic skill.

 

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.