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]; } //前往值为数组 查找到的标签内的内容
示例
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/">对于我</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) "对于我" }
保举:php效劳器
以上就是php若何猎取标签的值的具体内容,更多请存眷资源魔其它相干文章!
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
抱歉,评论功能暂时关闭!