php can be used to see the manual. This article mainly uses preg_match_all to test the effect of regular expression.
code instance:
[code]$html ='.lt; div id=.quot; biuuu.quot;.Gt; PHP self-taught network.Lt; /div.gt; dialectical; Such as biuuu, biuuu_2, biuuu_3, PHP self learning network, PHP self learning network 2 and PHP self learning network 3 (some commonly used catch methods are so matched)
analysis: string is a simple HTML element, each DIV element should be a ID and content, and is independent, first consider how to take out a ID value within DIV. And content, such as PHP self learning network, and then match other similar elements. A DIV needs to take two values, that is, two matching expressions, the first expression is used to match the ID value (biuuu), the second expression is used to match the content of the ID (the PHP self-study net), the regular expression commonly used expressions use parentheses, then the previous element will become the following form:
[code].lt; Div id=.quot; (biuuu).Quot;.Gt; (PHP self learning network).Lt; /div.gt;.Lt; div id=.quot; (expression 1).Quot; Or underline, that makes it easier to use parentheses, as follows:
[code] expression 1:.#91; a-zA-Z0-9_.#93; (representing matching size, letter, number and underline) how [/code] that matches expression 2, because the content of ID can be a desired character, but be careful not to match. With.Lt; or.Gt; characters, because if the matching of these two characters will match the DIV that is used later, it is necessary to exclude the elements that begin with the two characters, that is, the mismatch of the.Lt; or the.Gt; the characters, as follows:
[code] expression 2:.#91; ^.lt;.Gt;.#93; Thus, the subexpressions that need to be matched are implemented, but there is a need to match an expression of
: the following: the
[code] expression: /'.quot; (expression 1).Quot;'.gt; (expression 2).Lt; /div.gt; /[/code] pays attention to the double quote.Quot; and / needs to use the escape character to escape, and then the two preceding expressions As follows:
[code]'.quot; (.#91; a-z0-9_.#93;).Quot;'.gt; /.lt; div id=.quot; (.#91; a-z0-9_.#93;); As follows:
[code]$html ='.lt; div id=.quot; biuuu.quot;.Gt; PHP self-taught network.Lt; /div.gt;.Lt; div. Lt; /div.gt; / ', $html, $result';
var_dump ($result); [/code] results:
[code]array (3) {
.#91; T;.Gt; PHP self learning network 2.lt; /div.gt;.Quot;
.#91; 2.#93; =.gt; string (33).Quot; 2.#93; =.gt; string (7).Quot; biuuu_3.quot;
}
.#91; 2.#93; =.gt; array (3)