Jump to content
zeus69

Remove a HTML tag class DOM

Recommended Posts

HI,

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

$dom = new DOMDocument;
$dom->loadHTML($html, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);
$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"

Regards

Share this post


Link to post
Share on other sites

Okay. Are you saying that code doesn't do it? Because it looks like it would do it.

PS: If you want to remove a node, remove the node.

Share this post


Link to post
Share on other sites

I need what it does, for all class that contains "comments-box"

In regex, I do this, (. *?)comments-box(. *?). I don't know how to do with DOM

Share this post


Link to post
Share on other sites

You could take the code in your first post, make it a function and use the " wash, rinse, repeat" approach.

Share this post


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

Share this post


Link to post
Share on other sites

You want to do something. Your code already does it. What is the problem?

Share this post


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

Share this post


Link to post
Share on other sites

Okay. Then see the link in my first reply.

  • Thanks 1

Share this post


Link to post
Share on other sites

What @requinix said

foreach ($spanNodeList as $spanNode) {
    $spanNode->parentNode->removeChild($spanNode);
}

 

Share this post


Link to post
Share on other sites

Join the conversation

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

Guest
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.