PHP Regular Expressions (2)

Source: Internet
Author: User
Tags php regular expression preg
PHP regular expressions have two important methods: preg_match () and preg_replace (). A simple example is provided. To match this parameter, use the following pattern: $ str123123, 123123,123123, 2017123,234234; $ model ^ ([0-9] *), ([0-9] *),) * ([0-9] *) $; $ matcharray (); $ retpreg_m

PHP regular expressions have two important methods: preg_match () and preg_replace (). A simple example is provided. To match this parameter, use the following pattern: $ str = "123123,123123, 123123,2017123, 234234"; $ model = "/^ ([0-9] *). ([0-9] *),) * ([0-9] *) $/"; $ match = array (); $ ret = preg_m

PHP Regular Expression

There are two important methodsPreg_match ()AndPreg_replace ()For a simple example. The pattern used for matching is as follows:

$ Str = "123123,123123, 123123,2017123, 234234"; $ model = "/^ ([0-9] *), ([0-9] *) * ([0-9] *) $/"; $ match = array (); $ ret = preg_match ($ model, $ str, $ match ); $ match contains matched content.
Greedy matchAnd Non-Greedy matchSee the following example.
$ Eee = "(12)-123 23-(233)"; // greedy match example: this pattern can only match one. $ Preg = "/\(. * \)/"; $ match = array (); $ ans = preg_match_all ($ preg, $ eee, $ match); var_dump ($ match ); // The result is that only one pattern can be matched. Because the first one seems to be a complete parentheses, the entire result will be matched. // In non-Greedy match mode, you can find all the content in the brackets, but not the question mark after. $ Preg = "/\(.*? \)/"; $ Match = array (); $ ans = preg_match_all ($ preg, $ eee, $ match); var_dump ($ match );
Regular Expression replacement

Preg_replace ()You can replace the content in $ str. In the above example, we replace the number in the brackets of $ eee in China with the string haha. We can design the following method. Note:Preg_replace. $ Pattern and $ replace are both arrays. The matching modes are shown below.

Replace the matched content according to the regular expression rules. $ Eee = "(12)-123 23-(233)"; $ preg = array ("/\(.*? \)/"); $ Replace = array (" haha "); $ ans = preg_replace ($ preg, $ replace, $ eee); var_dump ($ ans );

The above is a relatively basic usage, and the following is a little more complicated.

$ Str = preg_replace ("/()(.*?) (<\/A>)/", '\ 1 \ 2 \ 3', $ str); // by learning the above expression, we can convert the previous example into this. $ Eee = "whie2ch (12)-123 23-function (233)"; $ preg = "/(\ w *)\(.*? \)/"; $ Replace =" \ 1word \ 2bb "; // replace the variable with word, replace the content in the brackets with bb \ 1 \ 2, which indicates the Matching content in the brackets. $ Ans = preg_replace ($ preg, $ replace, $ eee); var_dump ($ ans );

PHP Modifier

$ Eee = "Maid (1 \ n2)-123 n 23-function nhehe (23 \ n3) sfsdf (24 \ n5)"; $ preg = "/\(.*? \)/"; // $ Replace =" \ 1word \ 2bb "; $ match = array (); $ ans = preg_match_all ($ preg, $ eee, $ match ); var_dump ($ match); // in this way, the content in the brackets cannot be found. Because. Does not include line breaks. // Use the above $ preg as follows. $ Preg = "/\(.*? \)/S "; re-run the program and find that all contents match.

PHPOfModifierAs follows:

Modifier Description
S Enable. include line breaks to achieve cross-row matching.
U Let? Matched to greedy status by default
I Case Insensitive
U The mode string is treated as UTF8
M When this modifier is set, in addition to matching the beginning and end of the entire string, the row start (^) and the row end ($) also match the line break (\ n) respectively) after and before

PHP ModifierAs shown in the above program. Set/your_regrex/XX indicates your modifier. Select the modifier as needed.

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.