本文实例讲述了PHP正则匹配操作。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<?php $str = <<< EOT <a href= "www/app/a/2QRN7v" rel= "external nofollow" > <div class = "phonebg" > <img src= "http://www/template9/yunqingjian/jianjie/68.jpg" > <div class = "phoneclick" ></div> <p>幸福领地</p> </div> </a> <a href= "www/app/a/uqARNv" rel= "external nofollow" > <div class = "phonebg" > <img src= "http://www/template9/yunqingjian/jianjie/69.jpg" > <div class = "phoneclick" ></div> <p>一世情长</p> </div> </a> EOT; if (preg_match_all( '%<p.*?>(.*?)</p>%si' , $str , $matches )) { $arr [0][] = $matches [1]; } if (preg_match_all( '/src="([^<]*)" >/i' , $str , $matches )) { $arr [1][] = $matches [1]; } print_r( $arr ); exit ; ?> |
运行结果如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
Array ( [0] => Array ( [0] => Array ( [0] => 幸福领地 [1] => 一世情长 ) ) [1] => Array ( [0] => Array ( [0] => http: //www/template9/yunqingjian/jianjie/68.jpg [1] => http: //www/template9/yunqingjian/jianjie/69.jpg ) ) ) |
PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:
正则表达式在线测试工具:https://tool.zzvips.com/t/regex/
正则表达式在线生成工具:https://tool.zzvips.com/t/regcode/
希望本文所述对大家PHP程序设计有所帮助。