Jump to content

Remove a HTML tag class DOM

Recommended Posts


I have this code that deletes any div that contains class = "comments-box"

$dom = new DOMDocument;
$xp = new DOMXPath($dom);

// get the node list of div nodes with "target" class
$spanNodeList = $xp->query('//div[contains(@class, "comments-box")]');

foreach ($spanNodeList as $spanNode) {
$spanNode->parentNode->replaceChild($spanNode->firstChild, $spanNode);

I need it to delete all the divs that it also contains
class = "heycomments-box"
class = "sdfcomments-box"
class = "comments-boxsdfdfh"

class = "sdgsdgcomments-boxsdfdfh"


Link to post
Share on other sites

This time, your answer doesn't help me with anything, sorry

I need that if a div contains a class that includes word "comments-box". example: "23comments-box234"; "sdgcomments-boxsdg" and feels others, is eliminated with DOM code that I wrote above


Sorry and Thanks for your time lost with me

Link to post
Share on other sites

I need something like this with Dom

preg_replace('#<div class="(.*?)comments-box(.*?)">(.*?)</div>#', '', $html);  



Must delete all div, which contains class combinations that contains word comments-box. They can be billions. I can't do it manually

Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.