Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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

share|improve this question
    
What code are you using to interpret that {{ presentation_slides }} into something useful? Whatever it is, it's HTML-encoding your JSON. (As for the escaped slashes, they shouldn't be a problem -- JS doesn't mind them. It interprets \/ as just /.) – cHao Jul 1 '14 at 12:20
1  
here is The code: In the controller: return $this->render('AUIBookaBundle:Book:show.html.twig', array('presentation_slides' => $this->preparePresentation($id) )));int the twig template: <script> var presentation = {{ presentation_slides }} </script &quot;image&quot;:&quot;ara/web/uploads/images/slide/32.jpeg&quot; – anyavacy Jul 1 '14 at 12:38
    
Try using {{ presentation_slides|raw }}. It looks like that'll prevent the HTML-encoding, unless preparePresentation is doing the encoding itself. – cHao Jul 1 '14 at 12:40
1  
thank you sooooo much , it is working now – anyavacy Jul 1 '14 at 12:51

3 Answers 3

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);
share|improve this answer
    
thanks alot, that solved one problem – anyavacy Jul 1 '14 at 12:51

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 );

share|improve this answer
$presentation = json_encode($presentation_slides, JSON_UNESCAPED_SLASHES);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.