php打开修改并保存html文件

时间:2012-09-04 08:42:14

标签: php html dom removeclass

在php中我想打开一个html文件,删除div的内容(类区域)并保存。

$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());

它不起作用......

我的html文件显示:

<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>

我想得到这样的结果:

<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>

感谢您的帮助

更新

如何做同样的事情,但我的文件现在是xml?

我测试了这个:

    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();

现在我已经

<?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>

3 个答案:

答案 0 :(得分:4)

saveHTML只需将html作为字符串输出saveHTMLFile即可将其另存为文件。

答案 1 :(得分:4)

你几乎就在那里。您只需将Areas更改为Area,然后使用saveHtmlFile代替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");

这假设您要将HTML保存回原始文档。请注意,DOMXPath会在文档的顶部添加doctype,我认为没关系?

答案 2 :(得分:3)

您想要删除类Area的div,只需更改XPath查询:

$pDivs = $xpath->query(".//div[@class='Area']"); // not 'Areas'

当然,您还需要对结果做一些事情,例如:

echo htmlspecialchars($dom->saveHTML()); // prints the result