php使用正則表達式提取html標簽屬性
您正在編寫一個php函數(shù)用于過濾html,提取提取常用屬性,如style、class、href、target和alt。為了實現(xiàn)預(yù)期效果,需要對原提供的代碼進行修改。
使用正則表達式來提取所需的屬性值,以下為修正后的代碼塊:
$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 "替換的結(jié)果是 ".$result;
以上就是PHP正則表達式如何提取HTML標簽屬性值?的詳細內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!
HTML怎么學習?HTML怎么入門?HTML在哪學?HTML怎么學才快?不用擔心,這里為大家提供了HTML速學教程(入門課程),有需要的小伙伴保存下載就能學習啦!
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://www.400tele.com.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號