作者:放屁虫666_530 | 来源:互联网 | 2020-08-02 11:27
PHP可以使用正则表达式<$tag.*?$attr\".*?$value.*?\".*?>(.*?)<\$tag>is来获取指定标签的值,$tag-被查找的标签,$attr-被查找的属性名,$value-被查找的属性值。
PHP使用正则表达式获取指定标签的值:
//$html-被查找的字符串 $tag-被查找的标签 $attr-被查找的属性名 $value-被查找的属性值
function get_tag_data($html,$tag,$attr,$value){
$regex = "/<$tag.*?$attr=\".*?$value.*?\".*?>(.*?)<\/$tag>/is";
echo $regex."
";
preg_match_all($regex,$html,$matches,PREG_PATTERN_ORDER);
return $matches[1];
}
//返回值为数组 查找到的标签内的内容
示例
header("Content-type: text/html; charset=utf-8");
$temp = &#39;
&#39;;
$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社区 其它相关文章!