溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php獲取標簽值的方法

發(fā)布時間:2020-08-19 14:02:49 來源:億速云 閱讀:99 作者:小新 欄目:編程語言

小編給大家分享一下php獲取標簽值的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

PHP使用正則表達式獲取指定標簽的值:

//$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ù)組 查找到的標簽內(nèi)的內(nèi)容

示例

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" href="http://phpway.blog.163.com/">首頁</a>
</li>
<li class="w w1 selected">
<a class="i i1 fc01 h" hidefocus="true" href="http://phpway.blog.163.com/blog/">日志</a>
</li>
<li class="w w9">
<a class="i i9 fc01 h" hidefocus="true" href="http://phpway.blog.163.com/loftarchive/">LOFTER</a>
</li>
<li class="w w2">
<a class="i i2 fc01 h" hidefocus="true" href="http://phpway.blog.163.com/album/">相冊</a>
</li>
<li class="w w5">
<a class="i i5 fc01 h" hidefocus="true" href="http://phpway.blog.163.com/friends/">博友</a>
</li>
<li class="w w6">
<a class="i i6 fc01 h" hidefocus="true" href="http://phpway.blog.163.com/profile/">關(guān)于我</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) "關(guān)于我" }

以上是php獲取標簽值的方法的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI