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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:26:01+00:00 2026-06-04T14:26:01+00:00

I am attempting to use PHP to edit the DOM document tree. However, I

  • 0

I am attempting to use PHP to edit the DOM document tree. However, I am stuck. After loading the HTML, I want to remove every element EXCEPT a select few that I specify. (<p> and <b>, for example) How can I do this? Is it even possible?

Below is my current code:

<?php
$url = 'http://en.wikipedia.org/w/index.php?title=Elephant&action=render';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

$html = '<html>' . curl_exec($curl) . '</html>';
echo $html;

$document = new DOMDocument;
$document->loadHTML($html);

$allowed_elements = array(
    'a',
    'b',
    'i',
    'p',
);

$parent = $document->getElementsByTagName('html')->item(0);

foreach ($parent->getElementsByTagName('*') as $element)
{
    $node = strtolower((string)$element->nodeName);
    if (!in_array($node, $allowed_elements))
    {
        $element->parentNode->removeChild($element);
    }
}

echo $document->saveHTML();

curl_close($curl);
?>

My tinkering has shown me that it is possible to loop through the DOM tree, so I assume I could just loop through it. However, my code still isn’t working! I’m trying to get the plaintext Wikipedia article ultimately–if someone knows an alternate tool that I don’t have to write myself, that’ll be an acceptable answer.

Thanks!! 🙂

  • 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-04T14:26:04+00:00Added an answer on June 4, 2026 at 2:26 pm

    Try this:

    <?php
    $url = 'http://en.wikipedia.org/w/index.php?title=Elephant&action=render';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    
    $html = '<html>' . curl_exec($curl) . '</html>';
    curl_close($curl);
    
    $document = new DOMDocument('1.0');
    $document->loadHTML($html);
    
    $allowed_elements = array('a','b','i','p');
    $elems = array();
    
    $parent = $document->getElementsByTagName('html')->item(0);
    
    foreach ($parent->getElementsByTagName('*') as $element)
    {
        $node = (string)$element->nodeName;
        if(strtolower($node) == 'body'){
            continue;
        }
    
        $elems[] = $node;
    }
    
    $elems = array_values( array_unique( $elems ) );
    $elems = array_diff( $elems, $allowed_elements );
    $elems = array_values( array_unique( $elems ) );
    sort($elems);
    
    foreach( $elems as $elem ) {
        $parent1 = $parent->getElementsByTagName($elem);
        $length = $parent->getElementsByTagName($elem)->length;
    
        for($i=0;$i<$length;$i++) {
            $el = $parent1->item(0); // 0 is the index because after each `removeChild`, the next element shifts 1 position back.
            if( $el ) {
                $el->parentNode->removeChild($el);
            }
        }
    }
    
    echo $document->saveHTML();
    ?>
    

    $elementsToKeep – The array containg the list of items which are not to be deleted.

    Hope this helps.

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

Sidebar

Related Questions

I'm attempting to use the PHP DateTime class on a Solaris server with PHP
I am attempting to use PHP and cURL to: Access a web-form & maintain
I'm attempting to use OpenCV (on Android) in a project, however I'm struggling to
I'm attempting to use JS and PHP to dictate whether or not some navigation
I am attempting to use the following PHP class: <?php class Service { public
I'm creating a class and attempting to use it. However, the values I set
I'm attempting to return results from PHP to a div id=loginstart located on home.html,
I'm currently attempting to use PHP to transfer small files between our server and
I'm attempting to use php to read the source of a separate php file.
I'm attempting to use a sample function on the PHP manual website (posted by

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.