Php regular function 2preg_match_all-php tutorial

Source: Internet
Author: User
Php regular function 2 preg_match_all ???? Let's continue with the perl-style regular function preg_match_all .???? Function prototype :? & Lt ;? Php & gt; preg_match_all ($ pattern, $ subject, array & amp; $ matchesnull, $ flagsnull, $ offset php regular function 2 preg_match_all

? ? ? ? Let's continue with the perl-style regular function preg_match_all.

? ? ? ? Function prototype:

?

 preg_match_all ($pattern, $subject, array &$matches = null, $flags = null, $offset = null)
 

?

? ? ? ? Parameter: Identical to preg_match.

?

? ? ? ? Function: similar to preg_match, preg_match_all matches $ pattern in the $ subject string. Unlike preg_match, preg_match_all does not stop searching for the first matching result, search until the end of $ subject.

?

? ? ? ? Return value: according to the function, we can see that not only 0 or 1 is returned, preg_match_all searches for the entire $ subject until the end, and returns a few matching results. Let's look at an example with matching results.

?

 $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, which are http://www.sina.com.cn/abc/de/fg. Php?Fff. Html?Id = 1 the two red parts of the string. You will find that the elements in $ matches are also array types, $ matches [0] stores matching results, and $ matches [1] stores matching results of sub-regular 1, $ matches [2] stores the result of regular 2 matching. It may not be intuitive.

?



The Black Arrow is $ pattern regular match, and the Green Arrow is a child regular match.

Let's look at another example that does not match successfully.

 $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 =>     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.