The specific usage of the PHP function preg_match_all can be found inPHP function preg_match_all instance requirements: retrieve the ID and content of each DIV element, such as biuuu, biuuu_2, biuuu_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: A 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: php self-learning network, and 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>
Well, use the parentheses above to divide the areas to be matched. Next we will explain how to match the content in each expression. We suppose an ID may be a letter, number, or underline, this becomes simple, and can be implemented using brackets, as shown below:
Expression 1: [a-zA-Z0-9 _] + (indicating matching uppercase/lowercase letters, numbers, and underscores)
Then how to match expression 2, because the ID content can be any character, but note that it cannot match <or> characters, if the two characters are matched, the following DIV will be matched. Therefore, you need to exclude the elements starting with these two characters, that is, do not match with <or> characters, as shown below:
Expression 2: [^ <>] + (indicating unmatched <and> characters)
In this way, the PHP function preg_match_all needs to match the sub-expression, but it also needs to match an expression, the method is as follows:
Expression:/'"(expression 1)"'> (expression 2) </div>/
Note that the double quotation marks "And/must be escaped using escape characters, and then put the first two expressions in, as shown below:
- '"([a-z0-9_]+)"'>/
- < div id="([a-z0-9_]+)">
- ([^<>]+)
- < /div>/
In this way, a regular expression that matches the ID value and content of each DIV element is implemented. Then, the following is tested using the preg_match_all function:
- $ 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 )"
<Div id = "biuuu"> php self-learning Network </div>"
- [1] => string (33 )"
<Div id = "biuuu_2"> php self-learning Network 2 </div>"
- [2] => string (33 )"
<Div id = "biuuu_3"> php self-learning network 3 </div>"
- }
- [1] => array (3)
- {
- [0] => string (5) "biuuu"
- [1] => string (7) "biuuu_2"
- [2] => string (7) "biuuu_3"
- }
- [2] => array (3)
- {
- [0] => string (8) "php self-learning network"
- [1] => string (9) "php self-learning Network 2"
- [2] => string (9) "php self-learning network 3"
- }
- }
There are three expressions that show the matching values of each expression and store them as arrays. In this way, the ID and content of each DIV element are taken out. To use a regular expression, you need to know what you need and then match the regular expression with the regular expression. The idea is clear and the PHP function preg_match_all is used for output debugging, which is very convenient.