php open modify and save html file

随声附和 提交于 2019-12-01 11:41:01

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?

saveHTML just outputs the html as a string use saveHTMLFile to save it as a file.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!