Heim >Backend-Entwicklung >PHP-Tutorial >如何运用PHP函数preg_match_all测试正则表达式效果_PHP教程

如何运用PHP函数preg_match_all测试正则表达式效果_PHP教程

WBOY
WBOYOriginal
2016-07-15 13:30:271217Durchsuche

PHP函数preg_match_all的具体使用方法我们可以在PHP函数preg_match_all实例要求:分别将每一个DIV元素的ID和内容取出,如biuuu,biuuu_2,biuuu_3,php自学网,php自学网2和php自学网3(一些常用的抓站方法就是这样匹配的)

分析:字符串是一个简单的HTML元素,每一个DIV元素对应该一个ID和内容,并且是独立的,首先考虑如何取出一个DIV内的ID值和内容, 如:php自学网,然后匹配其它类似的元素。一个DIV中需要取出两个值,也就是两个匹配的表达式,第一个表达式用于匹配ID值(biuuu),第二个表 达式用于匹配ID的内容(php自学网),正则表达式常用的表达式使用小括号,那么前面的元素将会变成如下形式:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span class="tag-name">div</span><span> </span></span></span></li>
<li class="alt"><span><span class="attribute">id</span><span>=</span><span class="attribute-value">"(biuuu)"</span><span class="tag">></span></span></li>
<li class="alt"><span><span>(php自学网)</span></span></li>
<li class="alt"><span><span class="tag"><span class="tag-name">div</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span><span class="tag"><span class="tag-name">div</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"(表达式1)"</span><span class="tag">></span></span></span></li>
<li class="alt"><span><span>(表达式2)</span></span></li>
<li class="alt"><span><span class="tag"><span class="tag-name">div</span><span class="tag">></span><span>  </span></span></span></li>
</ol>

好,使用如上小括号把需要匹配的区域进行了划分,接下来就是如何匹配各个表达式内的内容,我们猜想一个ID可能是字母,数字或下划线,那这就变得简单了,使用中括号就可以实现,如下:

表达式1:[a-zA-Z0-9_]+ (表示匹配大小写字母,数字和下划线)

那如何匹配表达式2,因为ID的内容可以是任意的字符,但是要注意,不能匹配字符,因为如果匹配这两个字符将会把后面使用的DIV都匹配出来,因此需要排除这两个字符开始的元素,也就是不匹配以字符,如下:

表达式2:[^]+ (表示不匹配字符)

这样,PHP函数preg_match_all需要匹配的子表达式就实现了,但是还要需要匹配一个的表达式,方法如下:

表达式:/ '"(表达式1)"'>(表达式2)/

注意其中的双引号"和/需要使用转义字符转义,然后把前面两个表达式放进去,如下:

<ol class="dp-xml">
<li class="alt">
<span><span>'"([a-z0-9_]+)"'</span><span class="tag">></span></span><span><span>/</span></span>
</li>
<li class="alt"><span><span class="tag"><span class="tag-name">div</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"([a-z0-9_]+)"</span><span class="tag">></span></span></span></li>
<li class="alt"><span><span>([^</span><span class="tag"><span class="tag">></span><span>]+)</span></span></span></li>
<li class="alt"><span><span class="tag"><span class="tag-name">div</span><span class="tag">></span><span>/   </span></span></span></li>
</ol>

这样就实现一个匹配每一个DIV元素ID值和内容的正则表达式,然后使用preg_match_all函数测试如下:

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">html</span><span> = </span></span></li>
<li class="alt"><span><span class="attribute-value">'</span></span></li>
<li class="alt"><span><span class="attribute-value">php自学网</span></span></li>
<li class="alt"><span><span class="attribute-value"></span></span></li>
<li class="alt"><span><span class="attribute-value"></span></span></li>
<li class="alt"><span><span class="attribute-value">php自学网2</span></span></li>
<li class="alt"><span><span class="attribute-value"></span></span></li>
<li class="alt"><span><span class="attribute-value"></span></span></li>
<li class="alt"><span><span class="attribute-value">php自学网3</span></span></li>
<li class="alt"><span><span class="attribute-value">'</span><span>;   </span></span></li>
<li><span>preg_match_all('/</span></li>
<li><span class="tag"><span class="attribute">divsid</span><span class="tag-name">divsid</span><span>=</span><span class="attribute-value">"([a-z0-9_]+)"</span><span class="tag">></span></span></li>
<li>
<span>([^</span><span class="tag"><span class="tag">></span><span>]+)</span></span>
</li>
<li><span class="tag"><span class="tag-name">div</span><span class="tag">></span><span>/'</span><span>,$html,$result);   </span></span></li>
<li class="alt"><span>var_dump($result);  </span></li>
</ol>

PHP函数preg_match_all示例结果:

<ol class="dp-xml">
<li class="alt"><span><span>array(3) {   </span></span></li>
<li>
<span>[0]=</span><span class="tag">></span><span> array(3)   </span>
</li>
<li class="alt"><span>{   </span></li>
<li>
<span>[0]=</span><span class="tag">></span><span> string(30) "<br></span><span class="tag"><span class="tag-name">div</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"biuuu"</span><span class="tag">></span><span>php自学网</span><span class="tag"></span><span class="tag-name">div</span><span class="tag">></span><span>"   </span></span>
</li>
<li class="alt">
<span>[1]=</span><span class="tag">></span><span> string(33) "<br></span><span class="tag"><span class="tag-name">div</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"biuuu_2"</span><span class="tag">></span><span>php自学网2</span><span class="tag"></span><span class="tag-name">div</span><span class="tag">></span><span>"   </span></span>
</li>
<li>
<span>[2]=</span><span class="tag">></span><span> string(33) "<br></span><span class="tag"><span class="tag-name">div</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"biuuu_3"</span><span class="tag">></span><span>php自学网3</span><span class="tag"></span><span class="tag-name">div</span><span class="tag">></span><span>"   </span></span>
</li>
<li class="alt"><span>}   </span></li>
<li>
<span>[1]=</span><span class="tag">></span><span> array(3)   </span>
</li>
<li class="alt"><span>{   </span></li>
<li>
<span>[0]=</span><span class="tag">></span><span> string(5) "biuuu"   </span>
</li>
<li class="alt">
<span>[1]=</span><span class="tag">></span><span> string(7) "biuuu_2"   </span>
</li>
<li>
<span>[2]=</span><span class="tag">></span><span> string(7) "biuuu_3"   </span>
</li>
<li class="alt"><span>}   </span></li>
<li>
<span>[2]=</span><span class="tag">></span><span> array(3)   </span>
</li>
<li class="alt"><span>{   </span></li>
<li>
<span>[0]=</span><span class="tag">></span><span> string(8) "php自学网"   </span>
</li>
<li class="alt">
<span>[1]=</span><span class="tag">></span><span> string(9) "php自学网2"   </span>
</li>
<li>
<span>[2]=</span><span class="tag">></span><span> string(9) "php自学网3"   </span>
</li>
<li class="alt"><span>}   </span></li>
<li><span>}  </span></li>
</ol>

共有三个表达式,分别显示每一个表达式匹配的值,并以数组的形式存储,这样就把每一个DIV元素的ID和内容取出。使用正则表达式最主要还是要知道需要什么,然后跟椐需要进行匹配,并且思路清晰,并适当的借助PHP函数preg_match_all进行输出调试,非常方便。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446287.htmlTechArticlePHP函数preg_match_all的具体使用方法我们可以在 PHP函数preg_match_all实例要求:分别将每一个DIV元素的ID和内容取出,如biuuu,biuuu_2,biuuu_3,p...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn