Preg_match_all for global regular expression matching, this article mainly introduces the relevant information of the next preg_match_all, the need for friends can refer to the following
preg_match_all-the global regular expression matching description code is as follows: int Preg_match_all (string pattern, string subject, array matches [, int flags]) in Subject searches for all content that matches the regular expression given by the pattern and places the result in matches in the order specified by the flags. After the first match is searched, the next search starts at the end of the previous match. Pay special attention to 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 you use Preg_ The pattern_order results are sorted so that $matches [0] is an array of all pattern matches, $matches [1] is an array of strings that match the sub-patterns in the first parenthesis, and so on. (i.e. $matches[0] [0] for each of the full pattern matches, $matches [0] [1] for the second item in all pattern matching, $matches [1] [0] to match the first item in each parenthesis, $matches [1] [0] To match the second item in each parenthesis) the code is as follows: ]+> (. *) ]+>| 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 outputs: The code is as follows:
Example:, this is a test example:, the is a test so $out [0] contains a string that matches the entire pattern, $out [1] contains a string between a pair of HTML tags. If you use Preg_set_order to sort the results $matches [0] is an array of the first set of matches, $matches [1] is an array of the second set of occurrences, and so on. (that is, $matches[0] [0] is the string that matches the first set of matches, $matches [0] [1] for the first set of matches in the complete match in the first parenthesis of the string) code is as follows: ]+> (. *) ]+>| U ","
Example:This is a test ", $out, Preg_set_order);p rint $out [0][0].", ". $out [0][1]." \ n "; Print $out [1][0]. ",". $out [1][1]. " \ n ";?> This example outputs: The code is as follows:
Example:, Example:this is a test, which is a test in this example, $matches [0] is the first set of matching results, $matches [0][0] contains text that matches the entire pattern, $matches [0][1] contains text that matches the first sub-pattern And so on Similarly, $matches [1] is the second set of matching results, and so on. Preg_offset_capture If you set this tag, the matching result for each occurrence also returns its subordinate string offset. Note that this changes the value of the returned array so that each cell is also an array, where the first item is the matching string, and the second item is its offset in subject. This tag is available from PHP 4.3.0. If no token is given, it is assumed to be preg_pattern_order. Returns the number of times the entire pattern match (possibly 0), if an error returns FALSE. Example 1. Get all the phone number codes from a text as follows: Example 2. Search for matching HTML markup (greedy) code as follows: Bold textClick Me ";p reg_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 \ nthe ";}? > This example will output: code as follows: matched:
Bold textPart 1:
Part 2:bold Textpart 3:Matched:click mepart 1:part 2:click mepart 3:
http://www.bkjia.com/PHPjc/730223.html www.bkjia.com true http://www.bkjia.com/PHPjc/730223.html techarticle Preg_match_all for the global regular expression matching, this article mainly introduces the relevant information of the next preg_match_all, the need for friends can refer to the following preg_match_all for the global regular expression of the horse ...