Use the preg regular function in php and the phppreg Function

Source: Internet
Author: User
Tags preg

Use the preg regular function in php and the phppreg Function
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:

<? Php // Note: Regular/a. +? E/non-Greedy mode (because the quantifiers '+' are followed '? '), If/a. +? E/U is changed back to greedy mode preg_match ("/a. +? E/"," abcdefgabcdefgabcdefg ", $ out1); preg_match_all ("/a. +? E/"," abcdefgabcdefgabcdefg ", $ out2); var_dump ($ out1); var_dump ($ out2);/* 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. 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:
<? Php $ str = "http://www.baidu/.com? Url = www.sina.com/"; preg_match ("/http :(. *) com/", $ str, $ matches1); // greedy match var_dump ($ matches1); preg_match ("/http :(. *?) Com/", $ str, $ matches2); // non-Greedy match (the keyword '*' is followed '? ') Var_dump ($ matches2);/* 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. Differences between preg_match_all parameters PREG_PATTERN_ORDER (default) and PREG_SET_ORDER
<?phpecho('PREG_PATTERN_ORDER');preg_match_all("|<[^>]+>(.*)</[^>]+>|U",    "<b>start: </b><b>this is a test</b><b>end</b>",    $out1);var_dump($out1);echo('PREG_SET_ORDER');preg_match_all("|<[^>]+>(.*)</[^>]+>|U",    "<b>start: </b><b>this is a test</b><b>end</b>",    $out2, PREG_SET_ORDER);var_dump($out2);/*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 '<b>end</b>' (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 '<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) */?>

For more information, see preg_match_all.


How to Use the PHP function preg_match_all to test the regular expression effect

PHP function preg_match_all code example: php self-learning Network 2 </div <div id = "biuuu_3" php self-learning network 3 </div '; PHP function preg_match_all instance requirements: retrieve the ID and content of each DIV element, such as biuuu, biuuu_2, biuuuu_3, and php self-learning network, php self-learning Network 2 and php self-learning network 3 (some common website capturing methods are matched in this way) Analysis: string is a simple HTML element, each DIV element pair should have an ID and content independent. First, consider how to retrieve the ID value and content in a DIV, such as: php self-learning network, then match other similar elements. Two values must be extracted from a DIV, that is, two matching expressions. The first expression is used to match the ID value (biuuu ), the second form is used to match the content of the ID (php self-learning network). Regular Expressions use parentheses, and the preceding elements are changed to the following form: <div id = "(biuuu)" (php self-learning Network) </div <div id = "(expression 1)" (expression 2) </div expression 1: [a-zA-Z0-9 _] + (indicating matching uppercase/lowercase letters, numbers, and underscores) expression 2: [^ <] + (indicating not matching <and characters) This way, the PHP function preg_match_all needs to match the child expression, but it also needs to match an expression, as follows: expression:/'"(expression 1)"' (expression 2) /Note that the double quotes "And/must be escaped using escape characters, and then put the first two expressions in, as follows: '" ([a-z0-9 _] +) "'/<div id =" ([a-z0-9 _] +) "([^ <] +) </div/to implement a regular expression that matches the ID value and content of each DIV element. Then, use the preg_match_all function to test the function as follows: $ html = '<div id = "biuuu" php self-learning Network </div <div id = "biuuu_2" php self-learning Network 2 </div <div id = "biuuu_3" php self-learning network 3 </div '; preg_match_all ('/<divsiddivsid = "([a-z0-9 _] +)" ([^ <] +) </div/', $ html, $ result ); var_dump ($ result); PHP function preg_match_all example result: array (3) {[0] = array (3) {[0] = string (30) "php self-learning network" [1] = string (33) "php self-learning Network 2" [2] = string (33) "php self-learning network 3"} [1] = array (3) {[0] = string (5) "biuuu" [1] = string (7) "biuuu_2" [2] = string (7) "biuuu_3 &...... remaining full text>

Regular Expression of the preg_replace function in php

Write the following code:

<? Php
$ S1 = "<? Php \ $ var ['a'];?> ";
$ S2 = "<? Php \ $ var ['a'] ['bbb '];?> ";

$ Pa = '/<\? Php + \ $ ([_ a-zA-Z0-9] +) (\ [\ '[a-zA-Z0-9] + \' \] +) (\ [\ '[a-zA-Z0-9] + \' \]) *;? \?> /Si ';

Echo preg_replace_callback ($ pa, 'doreid', $ s2 );

Function doReplace ($ matches)
{
$ Resut = '<? Php $ ehs-> get ('. $ matches [1].')-> value ';
For ($ I = 2; $ I <count ($ matches); $ I ++)
{
$ Resut. = $ matches [$ I];
}
Return $ resut;
}

?>

// Simple code
<? Php
$ S1 = "<? Php \ $ var ['a'];?> ";
$ S2 = "<? Php \ $ var ['a'] ['bbb '] ['cccccc'];?> ";

$ Pa = '/<\? Php + \ $ ([_ a-zA-Z0-9] +) ([\ '\ S \'] + );? \?> /Si ';

Echo preg_replace_callback ($ pa, 'doreid', $ s2 );

Function doReplace ($ matches)
{
Return '<? Php $ ehs-> get ('. $ matches [1].')-> value'. $ matches [2];
}

?>

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.