Php preg regular function usage _ PHP Tutorial

Source: Internet
Author: User
Tags preg
Use the preg regular function in php. In php, the preg regular function uses 1. The difference between preg_match and preg_match_all is that preg_match matches only once. And preg_match_all match until the string is used by the preg regular function in php.
1. differences between preg_match and preg_match_all
The difference between preg_match and preg_match_all is that preg_match matches only once. Preg_match_all matches all until the string ends. Example:
 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. differences between greedy mode and non-greedy mode
For example, String str = "abcaxc ";
Patter p = "AB * c ";
Greedy match: Regular expressions tend to match the maximum length, that is, greedy match. For example, if the preceding pattern p is used to match the str string, the result is: abcaxc (AB * c ).
Non-greedy match: it is good to match the result with fewer matching characters. For example, if the preceding pattern p is used to match the string str, the result is: abc (AB * c ).
Example:
 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. differences between preg_match_all parameters PREG_PATTERN_ORDER (default) and PREG_SET_ORDER
 ]+>(.*)
 ]+>|U",    "start: this is a testend",    $out1);var_dump($out1);echo('PREG_SET_ORDER');preg_match_all("|<[^>]+>(.*)
 ]+>|U",    "start: this is a testend",    $out2, PREG_SET_ORDER);var_dump($out2);/*PREG_PATTERN_ORDERarray (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_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) */?>

For more information, see preg_match_all.

Comparison 1. differences between preg_match and preg_match_all: preg_match matches only once. And preg_match_all match until the string ends...

Related Article

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.