Please suggest me the PHP regex for preg_replace to remove all the attributes from tags in HTML code without removing the tags. But in hyper links all the attributes such as href, terget, rel should remain as is
Please refer the below example:
I already tried below regex with 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;
Filtered 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>
The intended output should look like:
<p>
<span>
<b>Some text here</b>
</span>
<a target="_blank" rel="nofollow" href="http://thebankexam.com/page/7017">Clickable Text</a>
</p>