教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php如何获取标签的值

php如何获取标签的值

发布时间:2020-06-19   编辑:jiaochengji.com
教程集为您提供php如何获取标签的值等资源,欢迎您收藏本站,我们将为您提供最新的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];
}
//返回值为数组 查找到的标签内的内容

示例

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如何获取标签的值的详细内容,更多请关注教程集其它相关文章!

  • 本文原创发布教程集,转载请注明出处,感谢您的尊重!
  • 您可能感兴趣的文章:
    php删除字符串中html标签的函数
    php删除html标签及字符串中html标签的代码
    PHP能写爬虫吗?(PHP实现爬虫技术示例)
    php如何获取标签的值
    Jquery为a标签的href赋值实现代码
    php过滤html标记的函数strip_tags用法举例(图文)
    php用strip_tags完整去除所有html标签的实例分享
    用php生成HTML文件的类
    PHP 获取Url参数的几种方法
    jQuery获取当前对象标签名称的方法

    [关闭]
    ~ ~