Introduction to preg_match_all

Source: Internet
Author: User

Preg_match_all is used for global regular expression matching. This article mainly introduces information about preg_match_all. For more information, see

Preg_match_all-perform global regular expression matching. The Code is as follows: int preg_match_all (string pattern, string subject, array matches [, int flags]) search all the content that matches the regular expression given by pattern in the subject and put the result in the matches in the order specified by flags. After the first match is found, the next search starts from the end of the previous match. Note that PREG_PATTERN_ORDER and PREG_SET_ORDER flags can be a combination of the following tags (it is meaningless to combine PREG_PATTERN_ORDER and PREG_SET_ORDER ): if you use PREG_PATTERN_ORDER to sort the results so that $ matches [0] is an array of all pattern matching, $ matches [1] is an array consisting of strings matched by the Child pattern in the first parentheses, and so on. ($ Matches [0] [0] indicates each item in all mode matching, and $ matches [0] [1] indicates the second item in all mode matching, $ matches [1] [0] is used to match the first item in each bracket. $ matches [1] [0] is used to match the second item in each bracket.) The Code is as follows: <? Php preg_match_all ("| <[^>] +> (. *) </[^>] +> | U "," <B> example: </B> <div align = left> this is a test </div> ", $ out, PREG_PATTERN_ORDER); print $ out [0] [0]. ",". $ out [0] [1]. "\ n"; print $ out [1] [0]. ",". $ out [1] [1]. "\ n";?> In this example, the output code is as follows: <B> example: </B>, <div align = left> this is a test </div> example :, this is a test. Therefore, $ out [0] contains a string that matches the entire pattern, and $ out [1] contains a string between a pair of HTML tags. If you use PREG_SET_ORDER to sort the results so that $ matches [0] is the array of the first group of matching items, $ matches [1] is the array of the second group of matching items, and so on. ($ Matches [0] [0] is the complete matching string in the first matching item, and $ matches [0] [1] is the string in the first matching item in the complete match in the First Matching column) the Code is as follows: <? Php preg_match_all ("| <[^>] +> (. *) </[^>] +> | U "," <B> example: </B> <div align = left> this is a test </div> ", $ out, PREG_SET_ORDER); print $ out [0] [0]. ",". $ out [0] [1]. "\ n"; print $ out [1] [0]. ",". $ out [1] [1]. "\ n";?> In this example, the output code is as follows: <B> example: </B>, example: <div align = left> this is a test </div>, in this example, $ matches [0] is the first matching result, and $ matches [0] [0] contains the text matching the entire pattern, $ matches [0] [1] contains text matching the first sub-mode, and so on. Similarly, $ matches [1] is the second group of matching results, and so on. PREG_OFFSET_CAPTURE if this tag is set, the offset of the affiliated string is also returned for each matching result. Note that this changes the value of the returned array, so that each unit is also an array. The first item is the matching string, and the second item is its offset in the subject. This tag is available from PHP 4.3.0. If no flag is provided, it is assumed to be PREG_PATTERN_ORDER. Returns the number of matching times (which may be zero) for the entire mode. If an error occurs, FALSE is returned. Example 1. The code for getting all phone numbers from a text is as follows: <? Php preg_match_all ("/\(? (\ D )? \)? (? (1) [\-\ s]) \ d-\ d/x "," Call 555-1212 or 1-800-555-1212 ", $ phones);?> Example 2. Search for the matched HTML Tag (greedy) Code as follows: <? Php // \ 2 is an example of reverse reference. Its meaning in PCRE is // must match the content in the second set of parentheses in the regular expression itself, in this example, // is ([\ w] + ). Because the string is enclosed in double quotes, you need to add a backslash. $ Html = "<B> bold text </B> <a href?howdy.html> click me </a>"; preg_match_all ("/(<([\ w] +) [^>] *> )(. *) (<\/\ 2>)/", $ html, $ matches); for ($ I = 0; $ I <count ($ matches [0]); $ I ++) {echo "matched :". $ matches [0] [$ I]. "\ n"; echo "part 1 :". $ matches [1] [$ I]. "\ n"; echo "part 2 :". $ matches [3] [$ I]. "\ n"; echo "part 3 :". $ matches [4] [$ I]. "\ n" ;}?> In this example, the output code is as follows: matched: <B> bold text </B> part 1: <B> part 2: bold textpart 3: </B> matched: <a href1_howdy.html> click me </a> part 1: <a href1_howdy.html> part 2: click mepart 3: </a>

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.