3

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)

1
  • Did you try saveHTMLFile()? Commented Aug 15, 2012 at 22:02

1 Answer 1

3

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();

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.