note 90852 deleted from domdocument.savehtml by danbrown
| From: | danbrown@php.net | Date: | Wed, 13 May 2009 13:35:33 +0000 |
| Subject: | note 90852 deleted from domdocument.savehtml by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-154553@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
To avoid script tags from being output as <script />, you can use the DOMDocumentFragment
class:
<?php
$doc = new DOMDocument();
$doc -> loadXML($xmlstring);
$fragment = $doc->createDocumentFragment();
/* Append the script element to the fragment using raw XML strings (will be preserved in their raw
form) and if succesful proceed to insert it in the DOM tree */
if($fragment->appendXML("<script type='text/javascript'
src='$source'></script>") {
$xpath = new DOMXpath($doc);
$resultlist = $xpath->query("//*[local-name() = 'html']/*[local-name() =
'head']"); /* namespace-safe method to find all head elements which are childs of the
html element, should only return 1 match */
foreach($resultlist as $headnode) // insert the script tag
$headnode->appendChild($fragment);
}
$doc->saveXML(); /* and our script tags will still be <script></script> */
?>