问题
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:
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?
回答2:
saveHTML just outputs the html as a string use saveHTMLFile to save it as a file.
回答3:
You want to remove the divs with class Area
, so simply change the XPath query:
$pDivs = $xpath->query(".//div[@class='Area']"); // not 'Areas'
And of course you will also need to do something with the results, for example:
echo htmlspecialchars($dom->saveHTML()); // prints the result
来源:https://stackoverflow.com/questions/12259785/php-open-modify-and-save-html-file