php打开修改并保存html文件 [英] php open modify and save html file

查看:152
本文介绍了php打开修改并保存html文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  $ 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> 
< head>
< title>< / title>
< link href =css.css =stylesheettype =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 =stylesheettype =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.0encoding =utf-8?> 
< html>
< head>
< title>< / title>
< link href =css.css =stylesheettype =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>


解决方案

您几乎就在那里。您只需将 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将在文档的顶部添加文档类型,我认为没关系?


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>

解决方案

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?

这篇关于php打开修改并保存html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆