PHP Regular function 2preg_match_all

Source: Internet
Author: User
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

?

  • 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.