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

  • SEARCH
  • Home
  • 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 8804769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:41:31+00:00 2026-06-14T01:41:31+00:00

I want remove some div with id or class which contain words comment or

  • 0

I want remove some div with id or class which contain words comment or share (like:<div id="comment">, <div class="header-comment">, <div id="comment-footer">, <div class="social-share">), something I use

preg_replace('/<div[^>]*(comment|share)[^>]*>(.*?)<\/div>/is', '', $htmls);

Not work. How to do a right regex? Here is some test code, I want to remove comment part and keep content and footer,

$htmls = <<<EOT
<div id="content">
     Main content.
</div>
<div id="comment">
    <ul>
        <li class="comment">
            <div class="header-comment">
                Comment:
                <span class="date-comment">8/11/2012, 21:25</span>
            </div>
            <h4>Some Text</h4>
            <p class="test-comment">Blah~~ Blah~~ Blah~~</p>
            <div class="share">
                <div class="vote">
                    <a class="vota yes" title="Like">2</a>
                    <a class="vota no" title="Unlike">0</a>
                </div>
            </div>
        </li>
        <li class="comment">
            <div class="header-comment">
                Comment:
                <span class="date-comment">8/11/2012, 23:08</span>
            </div>
            <h4>Other Text</h4>
            <p class="test-comment">Blah~~ Blah~~ Blah~~</p>
            <div class="share">
                <div class="vote">
                    <a class="vota yes" title="Like">4</a>
                    <a class="vota no" title="Unlike">0</a>
                </div>
            </div>
        </li>     
     </ul>
</div>
<div id="footer">
     Footer content.
</div>
EOT;

$htmls = preg_replace('/<div[^>]*(comment|share)[^>]*>(.*?)<\/div>/is', '', $htmls);
echo $htmls;
  • 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-14T01:41:32+00:00Added an answer on June 14, 2026 at 1:41 am

    What i think you should use is DomDocument try :

    $dom = new DOMDocument();
    $dom->loadHTML($htmls);
    $remove = array("comment","share");
    $removeList = array();
    foreach ( $dom->getElementsByTagName("div") as $div ) {
        if (in_array($div->getAttribute("class"), $remove) || in_array($div->getAttribute("id"), $remove)) {
            $removeList[] = $div;
        }
    }
    
    foreach ( $removeList as $div ) {
        $div->parentNode->removeChild($div);
    }
    
    $dom->formatOutput = true;
    echo "<pre>";
    echo htmlentities($dom->saveHTML());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to remove all <br> tags in the div class category alternate but
I've got some html that looks like this: <div> <span class=red>red text</span> some more
I want to remove some of the dashboard menu tabs on my wordpress site
I want to remove some elements from a vector and am using remove_if algorithm
I have this matrix of size 10000 by 1. I want to remove some
I want to during the page render remove some html from the page (don't
I want to use the Oracle REGEXP_REPLACE to remove some dots from a String.
I have some text that has HTML hyper-links in it. I want to remove
I am trying to do some string cleanup. I want to remove all the
I want to remove x_ from every id and every class in a ol.

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.