欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          PHP正則表達式抓取某個標簽的特定屬性值的方法

          php正則學了一些日子,抓了一些網(wǎng)站的數(shù)據(jù),從而發(fā)現(xiàn)每次都自己寫正則重新抓很麻煩,于是就想寫一個抓取特定標簽具有特定屬性值的接口通用,直接上代碼。

            //$html-被查找的字符串 $tag-被查找的標簽 $attr-被查找的屬性名 $value-被查找的屬性值  function get_tag_data($html,$tag,$attr,$value){  $regex = "/<$tag.*?$attr=".*?$value.*?".*?>(.*?)</$tag>/is";  echo $regex."<br>";  preg_match_all($regex,$html,$matches,PREG_PATTERN_ORDER);  return $matches[1];  }  //返回值為數(shù)組 查找到的標簽內的內容

          下面隨便給出一個例子

            header("Content-type: text/html; charset=utf-8");  $temp = '<ul class="noul clearfix">  <li class="w w0">  <a class="i i0 fc01 h" hidefocus="true" >首頁</a>  </li>  <li class="w w1 selected">  <a class="i i1 fc01 h" hidefocus="true" >日志</a>  </li>  <li class="w w9">  <a class="i i9 fc01 h" hidefocus="true" >LOFTER</a>  </li>  <li class="w w2">  <a class="i i2 fc01 h" hidefocus="true" >相冊</a>  </li>  <li class="w w5">  <a class="i i5 fc01 h" hidefocus="true" >博友</a>  </li>  <li class="w w6">  <a class="i i6 fc01 h" hidefocus="true" >關于我</a>  </li>  </ul>';  $result = get_tag_data($temp,"a","class","fc01");  var_dump($result);

          輸出結果為

            array(6) { [0]=> string(6) "首頁" [1]=> string(6) "日志" [2]=> string(6) "LOFTER" [3]=> string(6) "相冊" [4]=> string(6) "博友" [5]=> string(9) "關于我" }

          查看源碼可以看到

            array(6) {  [0]=>  string(6) "首頁"  [1]=>  string(6) "日志"  [2]=>  string(6) "LOFTER"  [3]=>  string(6) "相冊"  [4]=>  string(6) "博友"  [5]=>  string(9) "關于我"  }

          第一次寫blog好緊張哈哈哈,希望會對大家有用,也希望大家能指出代碼其中的問題,測試做的不是很多~~

          以上所述是小編給大家介紹的PHP正則表達式抓取某個標簽的特定屬性值的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對網(wǎng)站的支持!

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號