(.*?)<\/$tag>/is를 사용하여 지정된 태그를 가져올 수 있습니다. 값, $tag - 검색 중인 태그, $attr - 검색 중인 속성 이름, $value - 검색 중인 속성 값."/> (.*?)<\/$tag>/is를 사용하여 지정된 태그를 가져올 수 있습니다. 값, $tag - 검색 중인 태그, $attr - 검색 중인 속성 이름, $value - 검색 중인 속성 값.">

 >  기사  >  백엔드 개발  >  PHP에서 태그의 값을 얻는 방법

PHP에서 태그의 값을 얻는 방법

尚
원래의
2019-10-29 15:17:002507검색

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 = &#39;<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>&#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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.