Simple application of PHP function preg_match_all regular expression

time:13-03-26 forum:PHP tools and code
  The specific usage of the preg_match_all function reloaded from http://hi.baidu.com/sjow001/item/5d14ff0a662ed10b6d9048ee

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)

prev:Use the PHP function preg_match_all () t... next:The price of upstream gas has not been a...
post comment
Notice



approved   What day can "star children" be inte...   Ji'nan Qianfo Hill Xingguo monastery...   2017 SONY children's life is a conce...   Massaging the acupoints can relieve ...   Baby swimming pool built in Britain
rand   PHP filter replacement output digita...   PHP uses $_GET to get processing cod...   Fourteen experiences in programming ...   Using JSON to implement cross domain...   PHP good verification code
ads

other

Because all the contents of this site are reproduced from other media, the intention is to provide free services to the public, but it does not mean that this site agrees with its views, and can not be responsible for the authenticity of its verification, such as the copyright unit or individual of the manuscript does not want to publish on this site, please Contact me, I will immediately remove it, thank you. Contact: atseashawk@163.com