I have a span tag that has role attr button. What I want to achieve is to remove the whole span while matching not the span itself but the role attribute
$str = 'This is a test buton. <span id="UmniBooking_36" class="insideB" type="Form" style="cursor: pointer;color:" role="button" >Click here</span>';
$str = preg_replace('~<role="button"(.*?)</(.*?)>~Usi', "", $str);
I am doing something wrong but I cant figure out what.
2
Answers
I just realized that I forgot to add
(.*?)
before the role attrThat way it words fine.
Regex is not recommended for manipulating parsable HTML. DOMDocument can hand this task with intuitive, reliable native method calls.
Code: (Demo)
Output (creates valid HTML with a parent tag):