我有两个文件。一个是input.php,另一个是output.html。
内input.php文件中的代码如下:如何使用php将html标签写入另一个html文件?
<?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格式)
是否有任何解决方案的2个问题?或者还有其他方法可以实现我的目标吗? (在input.php文件我试过saveHTML()方法 - 还是不行)
你尝试'saveHTMLFile()'? – Musa 2012-08-15 22:02:27
谢谢,它的工作原理。 – 2012-08-16 00:09:32