Preg_match_all usage tips _ PHP Tutorial

Source: Internet
Author: User
Preg_match_all. Preg_match_all-perform Global Regular Expression Matching. The Copy code is as follows: intpreg_match_all (stringpattern, stringsubject, arraymatches [, intflags]) perform global regular expression matching in subjec preg_match_all

Description

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.

Special note: PREG_PATTERN_ORDER and PREG_SET_ORDER

Flags can be a combination of the following tags (note that it is meaningless to combine PREG_PATTERN_ORDER and PREG_SET_ORDER ):

If PREG_PATTERN_ORDER is used

Sort the results to make $ matches [0] an array that matches all modes, $ matches [1] An array consisting of strings that match 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, and $ matches [1] [0] is used to match the second item in each bracket)

The code is as follows:


Preg_match_all ("| <[^>] +> (.*) ] +> | U ","Example:

This is a test

", $ Out, PREG_PATTERN_ORDER );

Print $ out [0] [0]. ",". $ out [0] [1]. "\ n ";
Print $ out [1] [0]. ",". $ out [1] [1]. "\ n ";
?>

This example will output:

The code is as follows:


Example:,

This is a test


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 PREG_SET_ORDER is used

Sort the results so that $ matches [0] is the array of the first set of matching items, $ matches [1] is the array of the second set 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:


Preg_match_all ("| <[^>] +> (.*) ] +> | U ","Example:

This is a test

", $ Out, PREG_SET_ORDER );
Print $ out [0] [0]. ",". $ out [0] [1]. "\ n ";
Print $ out [1] [0]. ",". $ out [1] [1]. "\ n ";
?>

This example will output:

The code is as follows:


Example:, Example:

This is a test

, This is a test

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 flag 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. retrieve all phone numbers from a text

The code is as follows:


Preg_match_all ("/\(? (\ D )? \)? (? (1) [\-\ s]) \ d-\ d/x "," Call 555-1212 or 1-800-555-1212 ", $ phones );
?>

Example 2. search for matched HTML tags (greedy)

The code is as follows:


// \ 2 is an example of reverse reference. its meaning in PCRE is
// Match the content in the second set of parentheses in the regular expression itself. In this example
// It Is ([\ w] + ). Because the string is enclosed in double quotation marks
// Add a backslash.
$ Html ="Bold textClick me ";
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 ";
}
?>

This example will output:

The code is as follows:


Matched:Bold text
Part 1:
Part 2: bold text
Part 3:

Matched: click me
Part 1:
Part 2: click me
Part 3:

Http://www.bkjia.com/PHPjc/728085.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/728085.htmlTechArticlepreg_match_all-perform global regular expression matching instructions code: int preg_match_all (string pattern, string subject, array matches [, int flags]) in subjec...

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.