PHP preg regular function using _php tutorial

Source: Internet
Author: User
Tags preg

Use of preg regular function in PHP


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:
 
  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:
 
  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
 ]+> (. *)
 ]+>| U ","Start:This is a testEnd", $out 1); Var_dump ($out 1); Echo (' Preg_set_order ');p reg_match_all (" |<[^>]+> (. *)
 ]+>| U ","Start:This is a testEnd", $out 2, Preg_set_order); Var_dump ($out 2);/*preg_pattern_orderarray (size=2) 0 = array (size=3) 0 = St Ring 'Start:' (length=14) 1 = String 'This is a test' (length=21) 2 = String 'End' (length=10) 1 = array (size=3) 0 = = String ' Start: ' (length=7) 1 = String ' This is a test ' (le NGTH=14) 2 = String ' End ' (length=3) preg_set_orderarray (size=3) 0 = = Array (size=2) 0 = String 'Start:' (length=14) 1 = String ' Start: ' (length=7) 1 = = Array (size=2) 0 = String 'This is a test' (length=21) 1 = String ' This is a test ' (length=14) 2 = = Array (size=2) 0 = String 'End' (length=10) 1 = String ' End ' (length=3) */?>

Read MORE: Preg_match_all usage examples

http://www.bkjia.com/PHPjc/894184.html www.bkjia.com true http://www.bkjia.com/PHPjc/894184.html techarticle The difference between the preg regular function in PHP using 1.preg_match and Preg_match_all Preg_match and Preg_match_all differences is preg_match match only once. And the Preg_match_all all match until the string knot ...

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