PHP正則匹配操作簡單示例【preg_match_all應用】
本文實例講述了PHP正則匹配操作。分享給大家供大家參考,具體如下:
<?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;
?>
運行結果如下:
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款非常方便的正則表達式工具供大家參考使用:
JavaScript正則表達式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達式在線生成工具:
http://tools.jb51.net/regex/create_reg
更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《php正則表達式用法總結》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
解析php函數(shù)method_exists()與is_callable()的區(qū)別
本篇文章是對php中method_exists()與is_callable()的區(qū)別進行了詳細的分析介紹,需要的朋友參考下2013-06-06
使用ThinkPHP自帶的Http類下載遠程圖片到本地的實現(xiàn)代碼
Thinkphp是國人開發(fā)一個PHP框架,該框架相比國外的一些框架也毫不遜色。強大的ORM,插件,分組等功能讓人愛不釋手。2011-08-08
php數(shù)組比較實現(xiàn)查找連續(xù)數(shù)的方法
這篇文章主要介紹了php數(shù)組比較實現(xiàn)查找連續(xù)數(shù)的方法,實例分析了php數(shù)組查找及字符串操作的相關技巧,需要的朋友可以參考下2015-07-07

