Frage

I have two files. One is input.php, the other is output.html.
Inner input.php file the code is like:

<?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");   
?>

in the output.html file:

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

Actually, I want add the under output.html's "head". But now there are two problems.

1.even though the content can be written in the output.php fle, but it always get in the first line of the file, it makes the html page does not work.

2.the tag is always added as . (xml format). it does not work, I need (a html format)

Is there any solution for the 2 problems? Or is there any other way to achieve my goal? (in the input.php file I tried saveHTML()-still not work)

War es hilfreich?

Lösung

From the documentation

DOMDocument::save — Dumps the internal XML tree back into a file

So no wonder you get a XML representation. You should try DOMDocument::saveHTMLFile as has already been suggested in the comments or you could try echo $doc->saveHTML();

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top