Use of preg regular function in PHP

Source: Internet
Author: User
Tags preg

the difference between 1.preg_match and Preg_match_all
The difference between Preg_match and Preg_match_all is that preg_match matches only once. The Preg_match_all all match until the end of the string. Cases:
<?php//Note: Regular/a.+?e/is a non-greedy mode (because the quantifier ' + ' is appended with '? '), and if the use of/a.+?e/u is changed back to greedy mode Preg_match ("/a.+?e/", "ABCDEFGABCDEFGABCDEFG", $out 1);p reg_match_all ("/a.+?e/", "ABCDEFGABCDEFGABCDEFG", $out 2), Var_dump ($out 1); Var_dump ($out 2);/* Output: Array (size =1)  0 = String ' abcde ' (length=5) array (size=1)  0 = =    Array (size=3)      0 = String ' abcde ' (length =5)      1 = String ' ABCDE ' (length=5)      2 = String ' ABCDE ' (length=5) */?>
2. The difference between greedy and non-greedy modes
such as: String str= "ABCAXC";
Patter p= "Ab*c";
Greedy matching: Regular expressions tend to match the maximum length, which is called greedy matching. As above using pattern p to match string str, the result is matched to: Abcaxc (ab*c).
Non-greedy match: just match to the result is good, fewer matching characters. As above using pattern p to match string str, the result is matched to: ABC (AB*C).
Cases:
<?php$str = "http://www.baidu/.com?url=www.sina.com/";p reg_match ("/http: (. *) com/", $str, $matches 1); Greedy Match var_dump ($matches 1);p reg_match ("/http: (. *?)" com/", $str, $matches 2); Non-greedy match (quantifier ' * ' followed by '? ') Var_dump ($matches 2);/*array (size=2)  0 = String ' http://www.baidu/.com?url=www.sina.com ' (length=38)  1 = > String '//www.baidu/.com?url=www.sina. ' (length=30) Array (size=2)  0 = String ' http://www.baidu/.com ' (length=21)  1 = String '//www.baidu/. ' (length=13) */?>
3.preg_match_all parameter Preg_pattern_order (default) and Preg_set_order differences
<?phpecho (' Preg_pattern_order ');p reg_match_all ("|<[^>]+> (. *) </[^>]+>| U "," <b>start: </b><b>this is a test</b><b>end</b> ", $out 1); Var_dump ($out 1); echo (' Preg_set_order ');p reg_match_all ("|<[^>]+> (. *) </[^>]+>| U "," <b>start: </b><b>this is a test</b><b>end</b> ", $out 2, preg_set_order); var_  Dump ($out 2);/*preg_pattern_orderarray (size=2) 0 = = Array (size=3) 0 = String ' <b>start: </b> ' (length=14) 1 = String ' <b>this is a test</b> ' (length=21) 2 = String ' &LT;B&GT;END&LT;/B&G t; ' (length=10) 1-= array (size=3) 0 = = String ' Start: ' (length=7) 1 = = String ' This is a test ' (Leng TH=14) 2 = String ' End ' (length=3) preg_set_orderarray (size=3) 0 = = Array (size=2) 0 = String ' &l T;b>start: </b> ' (length=14) 1 = String ' Start: ' (length=7) 1 = = Array (size=2) 0 = String ' <b>this is a test</b> ' (length=21) 1 = = String ' This is a test ' (length=14) 2 = = Array (size=2) 0 = String ' <b>end</b> ' (length=10) 1 = String ' End ' (length=3) */?>

Read MORE: Preg_match_all usage examples

Use of preg regular function in PHP

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.