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 3940438
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:25:07+00:00 2026-05-20T00:25:07+00:00

I have a HTML document that I want to remove specific tags from, identified

  • 0

I have a HTML document that I want to remove specific tags from, identified by a specific class. The tags have multiple classes. A very simple example of the markup I have:

<style>.c{background-color:yellow}</style>
This is a <span class="a b c">string</span>.  
This is <span class="a b c">another string</span>.  
This is <span class="a b">yet another string</span>.

I want to be able to parse through that string (preferably using PHP’s DOMDocument?), finding only <span> tags with the class c so the result will look something like this:

<style>.c{background-color:yellow}</style>
This is a string.  
This is another string.  
This is <span class="a b">yet another string</span>.

Basically, I want to remove the tags around the text, but preserve the text on the document.

Update: I think I’m close, but it doesn’t work for me:

$test = '<style>.c {background-color:yellow;}</style>' .
'This is a <span class="a b c">string</span>.'.
'This is <span class="a b c">another string</span>.' .
'This is <span class="a b">yet another string</span>.';

$doc = new DOMDocument();
$doc->loadHTML($test);
$xpath = new DOMXPath($doc);
$query = "//span[contains(@class, 'c')]"; // thanks to Gordon
$oldnodes = $xpath->query($query);

foreach ($oldnodes as $oldnode) {
    $txt = $oldnode->nodeValue;
    $oldnode->parentNode->replaceChild($txt, $oldnode);
}

echo $doc->saveHTML();
  • 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-05-20T00:25:08+00:00Added an answer on May 20, 2026 at 12:25 am

    You’re close… Create a fragment for the children:

    $query = "//span[contains(concat(' ', normalize-space(@class), ' '), ' c ')]";
    $oldnodes = $xpath->query($query);
    
    foreach ($oldnodes as $node) {
        $fragment = $doc->createDocumentFragment();
        while($node->childNodes->length > 0) {
            $fragment->appendChild($node->childNodes->item(0));
        }
        $node->parentNode->replaceChild($fragment, $node);
    }
    

    Since each iteration will remove the $node, there’s no need to iterate (it’ll dynamically remove it from the result set since it’s no longer valid)…

    This will also handle the cases where you have more than just text inside the span:

    <span class="a b c">foo <b>bar</b> baz</span>
    

    Note the recent edit: I changed the xpath query to be more robust as now it will match only exact classes c rather than toc…

    What’s weird is that it allows you to remove in the iteration without affecting the results (I know it’s done that before, I just don’t know why here). But this is tested code and should be good.

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

Sidebar

Related Questions

I am constructing a large HTML document from fragments supplied by users that have
I want to capture phone numbers from an HTML document. I have used a
I have an HTML document that is using to embed a control. In some
I have an HTML (not XHTML) document that renders fine in Firefox 3 and
I have an <img> in an HTML document that I would like to highlight
I have a form on an HTML page with multiple submit buttons that perform
Okay, if I have a html document that looks a bit like this: <div
I have a problem that i want to search in the specific locations in
I'm using libxml2 to parse HTML. I want to remove certain formatting tags like
I have HTML that includes symbols such as the Trademark TM as superscript (™).

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.