+ -

php正则获取html任意标签方法

时间:2021-01-08

来源:互联网

标签: php

在手机上看
手机扫描阅读

php如何正则获取html任意标签?本篇文章主要教大家如何使用php正则获取html任意标签方法,一起来看看吧。

u=1875047515,1330064786&fm=26&gp=0.jpg

<?php

$temp = '

<div class="num">1</div>

<div class="num">2</div>

<div class="num">3</div>

<div class="num">4</div>

<div class="num1">3</div>

<div class="num2">4</div>

<div class="num">5</div>';

$result = get_tag_data($temp,"div","class","num");

print_r($result);

function get_tag_data($html,$tag,$class,$value){ 

    //$value 为空,则获取class=$class的所有内容

    $regex = $value ? "/<$tag.*?$class=\"$value\".*?>(.*?)<\/$tag>/is" :  "/<$tag.*?$class=\".*?$value.*?\".*?>(.*?)<\/$tag>/is";

    preg_match_all($regex,$html,$matches,PREG_PATTERN_ORDER); 

    return $matches[1];//返回值为数组 ,查找到的标签内的内容

}

以上就是php正则获取html任意标签方法希望对你的学习有所帮助。