本文實例講述了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相關內容感興趣的讀者可查看本站專題:《php正則表達式用法總結》、《PHP數組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。