skip to Main Content

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


  1. Chosen as BEST ANSWER

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

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

    That way it words fine.


  2. 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>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search