PHP DOM and JavaScript with HTML entities -


i'm using phps dom build html document.

at end of document, create script element.

if script has entites, specifically, < , >, these converted &lt; , &gt;

this problem if have strings containing characters (or in case regexs)

is there non hackish way (ie not string replacement) prevent behaviour in script tags only?

this not problem. characters encoded &lt; or &gt; if use domdocument::savexml(). if use domdocument::savehtml() < , > in <script> tag.

example:

<?php /**  * php dom , javascript html entities  *  * @link http://stackoverflow.com/q/18487515/367456  */  $doc = new domdocument("1.0"); $doc->loadxml('<head/>');  $javascriptcode = "\n  if (1 < 4) {\n    alert(\"hello\");\n  }\n";  $script = $doc->createelement('script'); $script->appendchild($doc->createcdatasection($javascriptcode));  $head         = $doc->getelementsbytagname('head')->item(0); $scriptinhead = $head->appendchild($script);  echo 'libxml: ', libxml_dotted_version, "\n"     , "\nxml:\n", $doc->savexml()     , "\nhtml:\n", $doc->savehtml() ; 

program output (demo (multi-version)):

libxml: 2.7.8  xml: <?xml version="1.0"?> <head><script><![cdata[   if (1 < 4) {     alert("hello");   } ]]></script></head>  html: <head><script>   if (1 < 4) {     alert("hello");   } </script></head> 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -