1. 程式人生 > >PHP 正則匹配 HTML 標籤

PHP 正則匹配 HTML 標籤

$str = '<div class="subnav-title-name">

<a href="http://www.autohome.com.cn/16/">一汽-大眾-捷達</a>

</div>';
//兩個rule 都可以匹配到資料
$rule = '/<div class="subnav-title-name">(.*?)<\/div>/ies';
$rule = '/<div class=\"subnav-title-name\".*?>([\s\S]*?)<\/div>/ies';

$res = preg_match_all($rule
,$str,$match); var_dump($match);

匹配結果如下:

array(2) {
  [0]=>
  array(1) {
    [0]=>
    string(106) "<div class="subnav-title-name">

<a href="http://www.autohome.com.cn/16/">一汽-大眾-捷達</a>

</div>"
  }
  [1]=>
  array(1) {
    [0]=>
    string(69) "

<a href="http://www.autohome.com.cn/16/
">一汽-大眾-捷達</a> " } }