Home > OS >  PHP remove element contain specific class
PHP remove element contain specific class

Time:01-28

I need help using preg_replace

First, I want to remove the anchor element and leave only the text. I use this regex : '#<a .*?>|#' and it seems to work.

then I want to remove an element that contains a certain class and I haven't found the right regex for it. I tried using this : '#<td *remove-this.*td>?#ms'; $str = preg_replace($anchor, '', $str); $str = preg_replace($remove, '', $str); return $str; } $tags = '<td > <a href="#" >Thank you</a> </td> <td > <a href="#" >Yuhuuu</a> </td>'; print_r( prp($tag) );

The output i want is:

<td >
Thank you
</td>

how to get it?

CodePudding user response:

function prp(string $html, array $classesToRemove): string {

  $doc = new DOMDocument();
  $doc->loadHTML($html);

  $htmlNode = $doc->childNodes[1];
  $bodyNode = $htmlNode->childNodes[0];

  foreach ($bodyNode->childNodes as $node) {
    if (( $node->nodeType === XML_ELEMENT_NODE ) && $node->tagName === 'td') {
      foreach ($node->attributes as $attribute) {
        if ($attribute->nodeName === 'class' && in_array($attribute->nodeValue, $classesToRemove)) {
          $bodyNode->removeChild($node);
        }
      }
    }

  }

  return str_replace([ '<body>', '</body>' ], '', $doc->saveHTML($bodyNode));

}

$tags = '
<td >
<a href="#" >Thank you</a>
</td>
<td >
<a href="#" >Yuhuuu</a>
</td>
';

print_r(prp($tags, [ 'hello there remove-this' ]));

CodePudding user response:

I've given up hope of expecting an answer here because instead of getting an answer my question got downvoted

but @lukas.j got me excited again and I'm trying to find information about PHP DOM

and this is the solution as expected

function prp($str)
{
    $str = "<tr>{$str}</tr>";

    $anchor = '#<a .*?>|</a>#';
    $str = preg_replace($anchor, '', $str);

    $doc = new DOMDocument;
    @$doc->loadHTML($str);
    $xpath = new DOMXPath($doc);
    $el = $xpath->query("//td[contains(@class, 'remove-this')]");
    if($el){
        $el = $el[0];
        $rel = $el->ownerDocument->saveXML($el);
        $str = str_replace($rel, '' , $str);
    }
    $tr = '#<tr.*?>|</tr>#';
    $str = preg_replace($tr, '', $str);

    return $str;
}

$tag = '<td >
            <a href="#" >Thank you</a>
        </td>
        <td >
            <a href="#" >Yuhuuu</a>
        </td>';

print_r( prp($tag) );
  •  Tags:  
  • Related