PHP regular function two Preg_match_all
? ? ? ? Continue with the Perl-style regular function Preg_match_all.
? ? ? ? Function Prototypes:
?
Preg_match_all ($pattern, $subject, array & $matches = null, $flags = NULL, $offset = NULL)
?
? ? ? ? Parameters: Exactly the same as Preg_match.
?
? ? ? ? function function: Similar to Preg_match, match $pattern in $subject string; Unlike Preg_match, Preg_match_all does not stop searching until the first result is matched, searching until the end of $subject.
?
? ? ? ? Return value: Depending on function function, you will see that not only return 0 or 1,preg_match_all will search the entire $subject until the end, there are several matching results returned. See an example with a matching result.
?
$url = ' http://www.sina.com.cn/abc/de/fg.php?fff.html?id=1 '; $matches = Array (); $pattern = '/(\) {1} [^.|?] +(\?) {1}/i '; $count = Preg_match_all ($pattern, $url, $matches); Var_dump ($count); Var_dump ($matches);
? output
int 2array (size=3) 0 = = Array (size=2) 0 = String '. php? ' (length=5) 1 = String '. html? ' (length=6) 1 = Array (size=2) 0 = = String '. ' (length=1) 1 = = String '. ' (length=1) 2 = Array (size=2) 0 = String '? ' (length=1) 1 = String '? ' (length=1)
? This example matches two results, namely HTTP://WWW.SINA.COM.CN/ABC/DE/FG. php? FFF. html? Id=1 the red two parts of the string. You will find that the elements in $matches are also array types, $matches [0] to store matching results, $matches [1] to store the results of the 1 match, $matches [2] to store the results of the regular 2 match. Maybe it's not very intuitive, so I can see that.
?
The black arrows are $pattern regular matches, and the green arrows are Zhong.
And look at an example of an unmatched success.
$url = ' http://www.sina.com.cn/abc/de/fg.php?fff.html?id=1 '; $matches = Array (); $pattern = '/(\) {1} [^.|?] +(\?) {2}/i '; $count = Preg_match_all ($pattern, $url, $matches); Var_dump ($count); Var_dump ($matches);
? output
int 0array (size=3) 0 = = Array (size=0) empty 1 = = Array (size=0) empty 2 =>< C13/>array (size=0) empty
?