Consider a php variable which contains a string of text, this text contains some html code, Say if I want to remove attributes from elements like <br>
but not from <spam>
the string contained on the php variable would be something like this:
<br id="foo" style="display:none">
<span id="bar">sometext</span>
<br id="bun" />
will become this:
<br>
<spam id="bar">sometext</span>
<br />
Note: I would like to have a regex where I could just change the tag name manually something like:
<?php
$str='<br id="foo" style="display:none">
<spam id="bar">sometext</span>
<br id="bun" />';
$tagname = 'br'
$regex = "regexpar1".$tagname."regexpart2";
echo preg_replace($regex,'',$str);
Thanks a lot
EDIT: SOLUTION (thanks to @avinash-raj)
$str='<br id="foo" style="display:none">
<span id="bar">sometext</span>
<br id="bun" />';
$tagname = 'br';
echo preg_replace('~(<'.$tagname.')\b[^>]*?(?=\h*\/?>)~','\1',$str);