SV: [PHP] Javascript backslash in php dom object
| From: | Uggla Henrik | Date: | Mon, 12 May 2014 08:44:35 +0000 |
| Subject: | SV: [PHP] Javascript backslash in php dom object | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-323286@lists.php.net to get a copy of this message | ||
Php doesn't like <![CDATA[ either:
<?php
$html = '<html><head><script
type="text/javascript"><![CDATA[]]></script></head><body></body></html>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$html2 = $dom->saveHTML();
echo $html2; //works not
?>
/Henrik
________________________________________
Från: Frank Arensmeier [farensmeier@gmail.com]
Skickat: den 9 maj 2014 15:30
Till: Uggla Henrik
Kopia: php-general@lists.php.net
Ämne: Re: [PHP] Javascript backslash in php dom object
9 maj 2014 kl. 14:51 skrev Uggla Henrik <Henrik.Uggla@kristianstad.se>:
> I'm trying to add some js code (from Openlayers3) to a php dom object. Echoing the html
> with the js code directly works but not after it is loaded into the dom. I get "SyntaxError:
> illegal character" in Firefox. I think it's the backslashes in the js code thats the
> problem. How could I make it work? I've tried htmlspecialchars but it didn't work.
>
> <?php
>
> $html = '<html><head><script
> type="text/javascript">'.file_get_contents("http://ol3js.org/en/master/build/ol.js").'</script></head><body></body></html>';
> $dom = new DOMDocument();
> $dom->loadHTML($html);
> $html2 = $dom->saveHTML();
> echo $html2; //works not
> // echo $html works
>
> ?>
>
> /HU
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
Hi!
Try to wrap the javascript code with "<![CDATA[” like so and see if that helps:
$html = '<html>
<head>
<script type="text/javascript">
<![CDATA[’.
file_get_contents("http://ol3js.org/en/master/build/ol.js”).
']]>
</script>
</head>
<body></body>
</html>’;
/frank