下面是PHP Manual中的内容
preg_match_all
preg_match_all — Perform a global regularexpression match
Report a bug
Description
int preg_match_all ( string $pattern ,string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [,int $offset = 0 ]]] )
Searches subject for all matches to theregular expression given in pattern and puts them in matches in the orderspecified by flags.
After the first match is found, thesubsequent searches are continued on from end of the last match.
Report a bug
Parameters
pattern
The pattern to search for, as a string.
subject
The input string.
matches
Array of all matches in multi-dimensionalarray ordered according to flags.
flags
Can be a combination of the following flags(note that it doesn't make sense to use PREG_PATTERN_ORDER together withPREG_SET_ORDER):
PREG_PATTERN_ORDER
Orders results so that $matches[0] is anarray of full pattern matches, $matches[1] is an array of strings matched bythe first parenthesized subpattern, and so on.
<?php
preg_match_all("|<[^>]+>(.*)</[^>]+>|U",
"<b>example: </b><div align=left>this is atest</div>",
$out, PREG_PATTERN_ORDER);
echo $out[0][0] . ", " .$out[0][1] . "\n";
echo $out[1][0] . ", " .$out[1][1] . "\n";
?>
The above example will output:
<b>example: </b>, <divalign=left>this is a test</div>
example: , this is a test
So, $out[0] contains array of strings thatmatched full pattern, and $out[1] contains array of strings enclosed by tags.
PREG_SET_ORDER
Orders results so that $matches[0] is anarray of first set of matches, $matches[1] is an array of second set ofmatches, and so on.
<?php
preg_match_all("|<[^>]+>(.*)</[^>]+>|U",
"<b>example: </b><divalign=\"left\">this is a test</div>",
$out, PREG_SET_ORDER);
echo $out[0][0] . ", " .$out[0][1] . "\n";
echo $out[1][0] . ", " .$out[1][1] . "\n";
?>
The above example will output:
<b>example: </b>, example:
<div align="left">this is atest</div>, this is a test
PREG_OFFSET_CAPTURE
If this flag is passed, for every occurringmatch the appendant string offset will also be returned. Note that this changesthe value of matches into an array where every element is an array consistingof the matched string at offset 0 and its string offset into subject at offset1.
If no order flag is given,PREG_PATTERN_ORDER is assumed.
offset
Normally, the search starts from thebeginning of the subject string. The optional parameter offset can be used tospecify the alternate place from which to start the search (in bytes).
Note:
Using offset is not equivalent to passingsubstr($subject, $offset) to preg_match_all() in place of the subject string,because pattern can contain assertions such as ^, $ or (?<=x). Seepreg_match() for examples.
Report a bug
Return Values
Returns the number of full pattern matches(which might be zero), or FALSE if an error occurred.
我重点介绍一下/U的使用
1、使用/U
<?php
$userinfo = "Name: <b>ZeevSuraski</b> <br> Title: <b>PHP Guru</b>";
preg_match_all("/<b>(.*)<\/b>/U",$userinfo, $pat_array);
printf("%s <br /> %s",$pat_array[0][0],$pat_array[1][0]);
?>
匹配了两次
$pat_array[0][0] = <b>ZeevSuraski</b>
$pat_array[0][1] = <b>PHPGuru</b>
而
$pat_array[1][0] = Zeev Suraski
$pat_array[1][1] = PHP Guru
2、不使用/U
<?php
$userinfo = "Name: <b>ZeevSuraski</b> <br> Title: <b>PHP Guru</b>";
preg_match_all("/<b>(.*)<\/b>/",$userinfo, $pat_array);
printf("%s <br /> %s",$pat_array[0][0],$pat_array[1][0]);
?>
匹配了一次,贪婪匹配,匹配最外的<b>....</b>
$pat_array[0] = <b>Zeev Suraski</b> <br>Title: <b>PHP Guru</b>
$pat_array[1] = Zeev Suraski</b><br> Title: <b>PHP Guru