【问题标题】:how to use php to write html tag to another html file?如何使用 php 将 html 标签写入另一个 html 文件?
【发布时间】:2012-08-15 21:49:42
【问题描述】:

我有两个文件。一个是input.php,另一个是output.html。
内部input.php文件的代码是这样的:

<?php

   $file="output.html";
   $doc=new DOMDocument('1.0');
   $doc->loadHTMLFile($file);
   $elements = $doc->getElementsByTagName('head');

   $jscript=$doc->createElement("script");
   $jstype=$doc->createAttribute("type");
   $jstText=$doc->createTextNode("text/javascript");
   $jstype->appendChild($jstText);
   $jscript->appendChild($jstype);
   $jssource=$doc->createAttribute("src");
   $jssText=$doc->createTextNode("xxxxx.js");
   $jssource->appendChild($jssText);
   $jscript->appendChild($jssource);
   $elements->item(0)->appendChild($jscript);
   $doc->save("output.html");   
?>

在 output.html 文件中:

<code>   
 <html>
   <head>
   </head>
   <body>
   </body>
 </html>
</code>

实际上,我想添加下 output.html 的“头”。但是现在有两个问题。

1.虽然内容可以写在output.php文件中,但总是在文件的第一行,导致html页面不工作。

2.标签始终添加为 . (xml 格式)。不行,我需要(html格式)

这两个问题有什么解决办法吗?或者有没有其他方法可以实现我的目标? (在 input.php 文件中我尝试了 saveHTML() - 仍然不起作用)

【问题讨论】:

  • 你试过saveHTMLFile()吗?

标签: php html writefile


【解决方案1】:

来自文档

DOMDocument::save — 将内部 XML 树转储回文件中

所以难怪你会得到一个 XML 表示。您应该按照 cmets 中的建议尝试 DOMDocument::saveHTMLFile,或者您可以尝试 echo $doc-&gt;saveHTML();

【讨论】:

    猜你喜欢
    • 2021-04-19
    • 1970-01-01
    • 2012-10-08
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    相关资源
    最近更新 更多