preg_match_all uses experience sharing

  • 2020-12-19 20:56:58
  • OfStack

preg_match_all - Performs global regular expression matching

instructions


int preg_match_all ( string pattern, string subject, array matches [, int flags] )

Search subject for all matches to the regular expressions given by pattern and place the results in matches in the order specified by flags.
After the first match is found, the next search begins 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 makes no sense to combine PREG_PATTERN_ORDER and PREG_SET_ORDER) :

If PREG_PATTERN_ORDER is used

Sort the results so that $matches[0] is the array of all pattern matches, $matches[1] is the array of strings matched by the subpatterns in the first bracket, and so on. (that is,$matches[0] [0] for every item in all pattern matches,$matches[0] [1] for the second item in all pattern matches,$matches[1] [0] for the first item in every bracket, and $matches[1] [0] for the second item in every bracket)


<?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"; 
?> 

This example will output:


<b>example: </b>, <div align=left>this is a test</div> 
example: , this is a test

Thus, $out[0] contains a string that matches the entire pattern, and $out[1] contains a string between 1 pair of HTML tags.
If PREG_SET_ORDER is used

Sort the results so that $matches[0] is the array of matches in group 1, $matches[1] is the array of matches in group 2, and so on. (That is,$matches[0] [0] is the string of the complete match in group 1, and $matches[0] [1] is the string of the complete match in bracket 1 in group 1.)


<?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"; 
?>

This example will output:


<b>example: </b>, example: 
<div align=left>this is a test</div>, this is a test

In this case, $matches[0] is the first set of matches, $matches[0][0] contains the text that matches the entire pattern, $matches[0][1] contains the text that matches the first subpattern, and so on. Again, $matches[1] is the second set of matches, and so on.

PREG_OFFSET_CAPTURE

If this tag is set, a string offset is also returned for each occurrence of a match. Note that this changes the value of the returned array so that each cell in it is also an array, where item 1 is the matching string and item 2 is its offset in subject. This tag is available from PHP 4.3.0 onwards.
If no markup is given, PREG_PATTERN_ORDER is assumed.
Returns the number of times the entire pattern matches (possibly zero), and FALSE if there is an error.

Example 1. Get all the phone numbers from some text


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

Example 2. Search for matching HTML tags (greedy)


<?php
// \\2  is 1 An example of a backward reference in  PCRE  The meaning of 
//  Must match the first in the regular expression itself 2 Group the contents within parentheses, in this case 
//  is  ([\w]+) . Because the string is in double quotes, you need to 
//  add 1 I have 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\n";
}
?> 

This example will output:


matched: <b>bold text</b>
part 1: <b>
part 2: bold text
part 3: </b>
matched: <a href=howdy.html>click me</a>
part 1: <a href=howdy.html>
part 2: click me
part 3: </a>


Related articles: