Reputation: 1697
I have a php array $presentation, and I am trying to assign it to a JavasScript variable. when I use json_encode i got this value:
{"image":"\/ara\/web\/uploads\/images\/slide\/34.jpeg","sound":"\/ara\/web\/uploads\/images\/slide\/34.mpga","content":"sentence"}
you see the back and forward slashes?
and this gets worst when I try to assign the variable to a javascript one: I get this:
"books":[{"image":"\/ara\/web\/uploads\/images\/slide\/32.jpeg"
and this is the code:
php:
$presentation_slide = json_encode($presentation_slides)
javascript:
<script>
var presentation = {{ presentation_slides }}
</script>
I am using sumfony2
Upvotes: 0
Views: 145
Reputation: 970
$presentation = json_encode($presentation_slides, JSON_UNESCAPED_SLASHES);
Upvotes: 0
Reputation: 1573
The json_encoding function has numerous flags that you can pass to it, which enable the function to parse particular character sets. The following call ought to solve the problems you are having
json_encode($presentation_slides, JSON_UNESCAPED_SLASHES | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP );
Upvotes: 1
Reputation: 1103
Maybe you should use the JSON_UNESCAPED_SLASHES option:
http://php.net/manual/en/function.json-encode.php
$presentation = json_encode($presentation_slides, JSON_UNESCAPED_SLASHES);
Upvotes: 1