请建议我使用preg_replace的PHP 正则表达式来删除 HTML 代码中标签的所有属性而不删除标签。但在超链接中,所有属性,如 href、terget、rel 应保持原样
请参考以下示例:
我已经用preg_replace尝试了下面的正则表达式:
$htmltext = '<p style="float: left;">
<span style="color: #ff0000;">
<b>Some text here</b>
</span>
<a target="_blank" rel="nofollow" href="http://thebankexam.com/page/7017">Clickable Text</a>
</p>';
$my_output = preg_replace("/<([a-z][a-z0-9]*)[^>]*?(\/?)>/i",'<$1$2>',$htmltext);
echo $my_output;
过滤后的输出($my_output):
<p>
<span>
<b>Some text here</b>
</span>
<a>Clickable Text</a> <!-- Check this hyper link href, rel and target gone -->
</p>
预期的输出应如下所示:
<p>
<span>
<b>Some text here</b>
</span>
<a target="_blank" rel="nofollow" href="http://thebankexam.com/page/7017">Clickable Text</a>
</p>