Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php将html标记写入另一个html文件?_Php_Html_Writefile - Fatal编程技术网

如何使用php将html标记写入另一个html文件?

如何使用php将html标记写入另一个html文件?,php,html,writefile,Php,Html,Writefile,我有两个文件。一个是input.php,另一个是output.html。 Inner input.php文件的代码如下: <?php $file="output.html"; $doc=new DOMDocument('1.0'); $doc->loadHTMLFile($file); $elements = $doc->getElementsByTagName('head'); $jscript=$doc->createElement(

我有两个文件。一个是input.php,另一个是output.html。
Inner 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下面的“head”。但现在有两个问题

1.即使内容可以写在output.php fle中,但它总是位于文件的第一行,这会使html页面无法工作

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

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

来自文档

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


因此,难怪您会得到XML表示。您应该按照注释中的建议尝试
DOMDocument::saveHTMLFile
,也可以尝试
echo$doc->saveHTML()

您是否尝试了
saveHTMLFile()