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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:24:20+00:00 2026-06-12T14:24:20+00:00

I have a part of html that is incompletely structured. Example: <div id=’notrequired’> <div>

  • 0

I have a part of html that is incompletely structured.
Example:

<div id='notrequired'>
    <div>
        <h3>Some examples :-)</h3>
        STL is a library, not a framework.
    </div> 
    </p>
    </a>
    <a target='_blank' href='http://en.wikipedia.org/wiki/Library_%28computing%29'>Read more</a>;
</div>
<a target='_blank' href='http://en.wikipedia.org/wiki/Library_%28computing%29'>Read more</a>";

As you can notice here I have unexpected </p> and </a> tags.

I tried a snippet of code to remove the <div id='notrequired'> and it works, but unable to handle it precisely.

Here’s the snippet code:

function DOMRemove(DOMNode $from) {
            $from->parentNode->removeChild($from);
        }

        $dom = new DOMDocument();
        @$dom->loadHTML($text); //$text contains the above mentioned HTML

        $selection = $dom->getElementById('notrequired');
        if($selection == NULL){
            $text = $dom->saveXML();
        }else{
            $refine = DOMRemove($selection);
            $text = $dom->saveXML($refine);
        }

The problem is $dom->saveXML saves as HTML content:

       <?xml version="1.0" standalone="yes"?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
        <html>

<body>
            <a target="_blank" href="http://en.wikipedia.org/wiki/Library_%28computing%29">Read more</a>

    </body>    
    </html>

All I only need is:

<a target='_blank' href='http://en.wikipedia.org/wiki/Library_%28computing%29'>Read more</a>

And not the <HTML> and <BODY> tags.

What am I missing? Any other way of doing it better?

  • 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-12T14:24:21+00:00Added an answer on June 12, 2026 at 2:24 pm

    Ok.. I guess I figured out a solution. Approach may not be right, but, it does the job!

    As Hakre pointed out that it’s the exact duplicate as innerHTML in PHP’s DomDocument?, It is not exact duplicate but it gave me a hint to use the idea. Thanks for suggestion.

    It helped me frame a solution below:

    function DOMRemove(DOMNode $from) {
        $from->parentNode->removeChild($from);
    }
    
    function DOMinnerHTML($element) 
    { echo "Ashwin";
        $innerHTML = ""; 
        $children = $element->childNodes; 
        foreach ($children as $child) 
        { 
            $tmp_dom = new DOMDocument(); 
            $tmp_dom->appendChild($tmp_dom->importNode($child, true)); 
            $innerHTML.=trim($tmp_dom->saveHTML()); 
        }
        return $innerHTML; 
    }
    
    $dom = new DOMDocument();
    $dom->preserveWhiteSpace = false; 
    @$dom->loadHTML($test);
    
    $a = $dom->getElementById('step');
    
    $b = DOMRemove($a);
    $c = $dom->saveXML($b);
    
    $domTable = $dom->getElementsByTagName("body"); 
    
    foreach ($domTable as $tables) 
    { 
        $x = DOMinnerHTML($tables); 
        echo $x; 
    }
    

    If the input is:

    <div id='step'>
        <div >
            <h3>Some examples :-(</h3>
            Blah blah blah...
        </div> </p>
        </a>
        <a target='_blank' href='#'>Read more</a>;
    </div>
    <div id='step2'>
        <div>
            <h3>Some examples :-) :-D</h3>
            Blah2 blah2 blah2...
        </div> </p> </a>
    </div>
    <a target='_blank' href='#'>Read more</a>
    <a target='_blank' href='#'>Read more</a>
    <a target='_blank' href='#'>Read more</a>
    

    The output, as expected, is:

    <div id="step2">
        <div>
            <h3>Some examples :-) :-D</h3>
            Blah2 blah2 blah2...
        </div> 
    </div>
    <a target="_blank" href="#">Read more</a>
    <a target="_blank" href="#">Read more</a>
    <a target="_blank" href="#">Read more</a>
    

    The solution works but may not optimal. Any thoughts?

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

Sidebar

Related Questions

I have some HTML that looks something like this: <div id=zoomed style=display:none; position:absolute;> <a
I have some HTML that looks like this: <div class=MyClass></div> <div class=MyClass></div> <div class=MyClass></div>
I have HTML that I need to extract a part number from, the HTML
I have a form that I don't render as part of HTML page but
I have a JLabel that needs to display some html-formatted text. However, I want
I have a html component that includes some javascript. The component is a file
I have some pages designed by someone else as simple HTML that I need
I have a snippet of HTML that is part of a form where there
I have a part of HTML source file that contains strings that I want
I have a snippet of HTML that is part of a form where there

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.