PHP preg Regular function use

Source: Internet
Author: User
Tags array end preg string
the difference between 1.preg_match and Preg_match_all
The difference between Preg_match and Preg_match_all is that preg_match only matches once. and 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 patterns and non-greedy patterns
such as: String str= "ABCAXC";
Patter p= "ab*c";
Greedy match: Regular expressions tend to match the maximum length, which is called greedy match. As above using pattern p to match the string str, the result is a match to: Abcaxc (ab*c).
Non-greedy match: just match to the result is good, fewer matching characters. As above using pattern p to match the string str, the result is a match 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)
 * *
?>
the difference between the 3.preg_match_all parameter Preg_pattern_order (default) and Preg_set_order
 -->]+> (. *)
 -->]+>u ","Start:This is a test End", $out 1);

Var_dump ($out 1);
Echo (' Preg_set_order '); Preg_match_all ("<[^>]+> (. *)
 -->]+>u ","Start:This is a test End", $out 2, Preg_set_order);

Var_dump ($out 2); /* Preg_pattern_order Array (size=2) 0 => Array (size=3) 0 => string '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 ' (length=14) 2 => string ' End ' (length=3) preg_set_order Array (size=3) 0 => Array (size=2) 0 =&G T 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) * *?>

Extended reading: Preg_match_all usages



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.