Remove tag with a specific attr from text PHP

39 Views Asked by At

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

There are 2 best solutions below

2
mickmackusa On BEST ANSWER

Regex is not recommended for manipulating parsable HTML. DOMDocument can hand this task with intuitive, reliable native method calls.

Code: (Demo)

$html = <<<HTML
This is a test buton. <span id="UmniBooking_36" class="insideB" type="Form" style="cursor: pointer;color:" role="button" >Click here</span>
HTML;

$dom = new DOMDocument;
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
foreach ($dom->getElementsByTagName('span') as $span) {
    if ($span->getAttribute('role') === 'button') {
        $parent = $span->parentNode;
        $parent->removeChild($span);
        $parent->nodeValue = trim($parent->nodeValue);
    }
}
echo $dom->saveHTML();

Output (creates valid HTML with a parent tag):

<p>This is a test buton.</p>
0
lStoilov On

I just realized that I forgot to add (.*?) before the role attr

$str = preg_replace('~<(.*?)role="button"(.*?)</(.*?)>~Usi', "", $str);

That way it works fine.