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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:34:22+00:00 2026-06-10T18:34:22+00:00

in php I would like to open a html file, delete the content of

  • 0

in php I would like to open a html file, delete the content of the div(class Areas) and save it.

$dom = new DOMDocument;
$dom->loadHTMLFile( "temp/page".$y.".xhtml" );
$xpath = new DOMXPath( $dom );
$pDivs = $xpath->query(".//div[@class='Areas']");
foreach ( $pDivs as $div ) {
  $div->parentNode->removeChild( $div );
}
echo htmlspecialchars($dom->saveHTMLFile());

It doesn’t work…

My html file look :

<html>
 <head>
  <title></title>
  <link href="css.css" rel="stylesheet" type="text/css" />
 </head>
 <body>
   <div style="height:998px;">
    <img src="images/bg004.jpg" />
     <div class="class1">
         <div class="class2"></div>
         <div class="class2"></div>
    </div>
    <div class="Areas">
         <div class="Area"><a href="index.html"></a></div>
         <div class="Area"><a href="index.html"></a></div>
         <div class="Area"><a href="index.html"></a></div>
    </div>
   </div>
  </body>
</html>

I would like to have this result :

<html>
 <head>
  <title></title>
  <link href="css.css" rel="stylesheet" type="text/css" />
 </head>
 <body>
   <div style="height:998px;">
    <img src="images/bg004.jpg" />
     <div class="class1">
         <div class="class2"></div>
         <div class="class2"></div>
    </div>
    <div class="Areas">

    </div>
   </div>
  </body>
</html>

Thank for your help

UPDATE

How to do the same thing but my file is now a xml ?

I test this :

    copy("temp/page".$y.".xhtml", "/temp/page".$y.".xml");
$dom = new DOMDocument;
$dom->load( "temp/page".$y.".xml" );
$xpath = new DOMXPath( $dom );
$pDivs = $xpath->query(".//div[@class='Area']");
foreach ( $pDivs as $div ) {
    $div->parentNode->removeChild( $div );
}
$dom->savexml();

And I have now

<?xml version="1.0" encoding="utf-8"?>
<html>
 <head>
  <title></title>
  <link href="css.css" rel="stylesheet" type="text/css" />
 </head>
 <body>
   <div style="height:998px;">
    <img src="images/bg004.jpg" />
     <div class="class1">
         <div class="class2"></div>
         <div class="class2"></div>
    </div>
    <div class="Areas">
         <div class="Area"><a href="index.html"></a></div>
         <div class="Area"><a href="index.html"></a></div>
         <div class="Area"><a href="index.html"></a></div>
    </div>
   </div>
  </body>
</html>
  • 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-10T18:34:24+00:00Added an answer on June 10, 2026 at 6:34 pm

    You were very nearly there. You just needed to change Areas to Area and then use saveHtmlFile instead of saveHTML:

    $dom = new DOMDocument;
    $dom->loadHTMLFile( "temp/page".$y.".xhtml" );
    $xpath = new DOMXPath( $dom );
    $pDivs = $xpath->query(".//div[@class='Area']");
    foreach ( $pDivs as $div ) {
      $div->parentNode->removeChild( $div );
    }
    $dom->saveHTMLFile("temp/page".$y.".xhtml");
    

    This is assuming you want to save the HTML back to the original document. Do note that DOMXPath will add a doctype to the top of your document, I assume that’s okay?

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

Sidebar

Related Questions

I would like to provide a PHP hanging request that will stay open until
Using PHP I would like to be able to list root's crontab. I am
Using Php I would like to extract the current Url of a page including
I'm not very good at PHP and would like to have a PHP function
I'm interested in what the code (command line and php) would look like where
I'm developing a PHP game and would like to post players highscores to their
I am running a php website and would like to pull in the most
I have two times in PHP and I would like to determine the elapsed
We have a project (Web/PHP) where we would like users to access their files,
I have a time stamp I created with PHP that I would like to

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.