php open modify and save html file

后端 未结 3 706
忘了有多久
忘了有多久 2021-01-23 13:43

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.         


        
3条回答
  •  没有蜡笔的小新
    2021-01-23 14:36

    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?

提交回复
热议问题