Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8701859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:30:19+00:00 2026-06-13T02:30:19+00:00

I have a HTML document (string) which contains a div with the class foo:

  • 0

I have a HTML document (string) which contains a div with the class “foo”:

<html>
<head>
  ...
</head>
<body>
<div class="whatever">Blabla</div>
<div>
   <span>Text</span>
</div>
<table>
   <tr><td><div class="foo">GARBAGE</div></td></tr>
</table>
</body>

I only would like to remove all divs with class of “foo” and this is what I have so far:

$doc = new DOMDocument();
$doc->loadHTML($myhtml);
$xpath = new DOMXpath($doc);
$all = $xpath->query("/html");

$result = remove_elements_with_class('foo', $all);

How does the remove_elements_with_class function look like?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:30:20+00:00Added an answer on June 13, 2026 at 2:30 am

    After:

    $xpath = new DOMXpath($doc);
    

    You need to:

    1. Select all the nodes that you want to remove
    2. Call DOMNode::removeChild() on those nodes

    So, to accomplish the first task, you can issue an XPath query that finds all of the <div> nodes whose class is foo. That query would look like:

    //div[contains(concat(' ', @class, ' '), ' foo ')]
    

    Note that this handles the cases where an element can have more than one class, i.e. foo bar baz and baz foo bar. If this is undesirable, and you only want to match the class exactly (so now only a class with exactly foo will match), the query becomes:

    //div[@class = 'foo']
    

    And, in PHP, this becomes:

    $nodes = $xpath->query( "//div[contains(concat(' ', @class, ' '), ' foo ')]");
    

    From here, you have all the nodes you want to remove in $nodes, so just iterate over them, and remove them from the document by grabbing the <div>‘s parent node, and removing its child node:

    foreach( $nodes as $node) {
        $node->parentNode->removeChild( $node);
    }
    

    That’s all it takes! You can see it working in this demo.

    Edit: To keep the <div> and just remove the contents, set the node’s nodeValue attribute to an empty string:

    foreach( $nodes as $node) {
        $node->nodeValue = '';
    }
    

    You can see it working in this updated demo. You could also replace the <div> with a newly created <div>, as that approach seems more bulletproof, but this should work for your use-case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a HTML document with the below setup: <div class=main-div style=padding: 5px; border:
Suppose I have a string which contains an HTML file. How do I get
I have an HTML document as a string I want to search for a
In Java I have an arbitrary HTML document as a string. For simplicity, say:
I have an HTML document where I have two different tables. One is class
I have an HTML document where I would like to semantically group text to
I am having problems echoing a string which contains a HTML select element with
In my HTML I have a content div that contains articles created by a
I am extracting some text from html which is passed as a string. The
I have a string of HTML that I use as jQuery input document. //

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.