如何使用php将html标记写入另一个html文件?
我有两个文件。一个是input.php,另一个是output.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(
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()
?