php使用正则表达式提取html标签属性
您正在编写一个php函数用于过滤html,提取提取常用属性,如style、class、href、target和alt。为了实现预期效果,需要对原提供的代码进行修改。
使用正则表达式来提取所需的属性值,以下为修正后的代码块:
$re = '/\bon\w+=([\'"]).*?\1/m'; $str = '<strong style="white-space: normal;" class="123" onload="asdasdas()"> </strong><div class="ccc">aaaaa</div> <p style="white-space: normal;">bbbbb</p> <strong class="123" style="white-space: normal;" onload="asdasdas()">12313123 </strong> <strong onload=\'asdasdas()\'>eeeeee </strong><a href="http://www.xxx.com" target="_blank" class="aaaa">链接链接</a><p>ffff</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">PHP免费学习笔记(深入)</a>”;</p>'; $subst = ''; $result = preg_replace($re, $subst, $str); echo "替换的结果是 ".$result;
以上就是PHP正则表达式如何提取HTML标签属性值?的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号