php判断 html标签

php判断 html标签,第1张

$html="这里是html标签内容"

$is_div=preg_match_all("/<div class=\"ABC\">(.+?)<\/div>/is", $html, $div)

if($is_div){

foreach($div as $d){//循环读取带有class=ABC的标签

$d_str=$d[0]//得到div字符串

$is_span=preg_match_all("/<span>(.+?)<\/span>/is", $d_str, $s)

if($is_span){//如果存在span标签,则执行替换

$new_d_str=str_replace($d_str,"class=\"DEF\"","class=\"ABC\"")//替换ABC为DEF

$html=str_replace($html,$new_d_str,$d_str)//替换

}

}

}

//$html就是得到替换后的html

在PHP文件中,可以先对html标签符号进行转义替换,然后做输出,其中 $text 是从表单接收到的 input 的 value 值:

<?php

$text=str_replace("<","<",$text) //替换<

$text=str_replace(' " ',""",$text) //替换"

$text=str_replace(">",">",$text) //替换>

echo $text

?>

此时页面输出的就是上面的value,未经Html解析过的。

<? php

$str = '<a href="/p/3729597758" title="【爱心反馈】四川色达色达中学反馈贴" target="_blank" class="j_th_tit">【爱心反馈】四川色达县色达中学反馈贴</a>'

preg_match_all('/href="(.*?) title="(.*?)"/is', $str, $arr)//正则匹配

print_r($arr)#打印匹配结果


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/7250297.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-03
下一篇 2023-04-03

发表评论

登录后才能评论

评论列表(0条)

保存