删除 PHP 中的所有属性,但按原样保留 Href、Terget、rel 中的链接


remove all attributes in php but keep href, terget, rel in link as is

请向我建议使用 PHP 正则表达式,以便preg_replace从 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_输出):

<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>
preg_replace('/<a's+[^>]*href's*='s*"([^"]+)"[^>]*>/', '<a href="'1">', $html);