note 90852 added to domdocument.savehtml
| From: | Anonymous at osu1 dot php dot net | Date: | Wed, 13 May 2009 02:35:28 +0000 |
| Subject: | note 90852 added to domdocument.savehtml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-154530@lists.php.net to get a copy of this message | ||
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> */
?>
----
Server IP: 213.249.64.162
Probable Submitter: 86.88.83.116
----
Manual Page -- http://www.php.net/manual/en/domdocument.savehtml.php
Edit -- https://master.php.net/note/edit/90852
Del: integrated -- https://master.php.net/note/delete/90852/integrated
Del: useless -- https://master.php.net/note/delete/90852/useless
Del: bad code -- https://master.php.net/note/delete/90852/bad+code
Del: spam -- https://master.php.net/note/delete/90852/spam
Del: non-english -- https://master.php.net/note/delete/90852/non-english
Del: in docs -- https://master.php.net/note/delete/90852/in+docs
Del: other reasons-- https://master.php.net/note/delete/90852
Reject -- https://master.php.net/note/reject/90852
Search -- https://master.php.net/manage/user-notes.php