zeus69 Posted November 11, 2019 Share Posted November 11, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/309514-remove-a-html-tag-class-dom/ Share on other sites More sharing options...
requinix Posted November 11, 2019 Share Posted November 11, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/309514-remove-a-html-tag-class-dom/#findComment-1571478 Share on other sites More sharing options...
zeus69 Posted November 11, 2019 Author Share Posted November 11, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/309514-remove-a-html-tag-class-dom/#findComment-1571480 Share on other sites More sharing options...
Barand Posted November 11, 2019 Share Posted November 11, 2019 You could take the code in your first post, make it a function and use the " wash, rinse, repeat" approach. Quote Link to comment https://forums.phpfreaks.com/topic/309514-remove-a-html-tag-class-dom/#findComment-1571481 Share on other sites More sharing options...
zeus69 Posted November 11, 2019 Author Share Posted November 11, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/309514-remove-a-html-tag-class-dom/#findComment-1571482 Share on other sites More sharing options...
requinix Posted November 11, 2019 Share Posted November 11, 2019 You want to do something. Your code already does it. What is the problem? Quote Link to comment https://forums.phpfreaks.com/topic/309514-remove-a-html-tag-class-dom/#findComment-1571484 Share on other sites More sharing options...
zeus69 Posted November 11, 2019 Author Share Posted November 11, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/309514-remove-a-html-tag-class-dom/#findComment-1571486 Share on other sites More sharing options...
requinix Posted November 11, 2019 Share Posted November 11, 2019 https://3v4l.org/040Pe YOUR CODE WORKS FINE. WHAT IS THE PROBLEM? Quote Link to comment https://forums.phpfreaks.com/topic/309514-remove-a-html-tag-class-dom/#findComment-1571488 Share on other sites More sharing options...
zeus69 Posted November 11, 2019 Author Share Posted November 11, 2019 now I understand, it was my mistake It must delete everything HTML inside the div that includes class comments-box (not just div) http://prntscr.com/pvinrw Quote Link to comment https://forums.phpfreaks.com/topic/309514-remove-a-html-tag-class-dom/#findComment-1571489 Share on other sites More sharing options...
requinix Posted November 11, 2019 Share Posted November 11, 2019 Okay. Then see the link in my first reply. 1 Quote Link to comment https://forums.phpfreaks.com/topic/309514-remove-a-html-tag-class-dom/#findComment-1571490 Share on other sites More sharing options...
Psycho Posted November 13, 2019 Share Posted November 13, 2019 What @requinix said foreach ($spanNodeList as $spanNode) { $spanNode->parentNode->removeChild($spanNode); } Quote Link to comment https://forums.phpfreaks.com/topic/309514-remove-a-html-tag-class-dom/#findComment-1571558 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.