How to escape text coming from PHP in JavaScript?

Go To StackoverFlow.com

3

I am using jPlayer to display some videos and I create the playlist on the fly in a php foreach statement:

var playlistacting = 
            [
        <?php foreach($this->result as $val){?>
                {
                    title:  '<?php echo $val->getTitle();?>',
                    artist: '<?php echo $val->getDes();?>',
                    poster: "<?php echo $val->getVideoId();?>.jpg",
                    thumb:  "<?php echo $val->getVideoId();?>.jpg",
                    flv:    "<?php echo $val->getVideoId();?>.flv",
                },
        <?php }?>
];

and $val->getDes() example would be I have a one-hour (approximately) solo musical revue called "Manhattan With A Twist". The entire...

the error I get is

unterminated string literal
[Break On This Error]   

artist: 'I have a one-hour (approximately)solo musical revue called "Manhattan W...

jquery.js (line 2, col 32)

and is poining to the ' at the beginning of the string.

I can do this: title: "<?php echo htmlspecialchars($val->getTitle());?>",

and I will get the same error but with " instead of '.

I'm not sure what is happening here. Is it complaining about that ' not being escaped or what?

Any ideas?

2012-04-04 20:52
by Patrioticcow
Have you considered using <code>json_encode()</code> - NullUserException 2012-04-04 20:54
Or addslashes() - Phix 2012-04-04 20:55
hmmm, let me tr - Patrioticcow 2012-04-04 20:55


4

simply use json_encode:

var playlistacting = <?php echo json_encode($this->result);?>;
2012-04-04 20:55
by Dr.Molle
it worked. Why - Patrioticcow 2012-04-04 20:56
Why shouldn't it - Dr.Molle 2012-04-04 20:57
i can see using it on a array, but why is working on this strin - Patrioticcow 2012-04-04 20:59
It's not working on a string, it's working on an array/object that contains strings - Dr.Molle 2012-04-04 21:00
I don't know why this worked. json_encode() is the right answer, but from what you've shown us of your result object, it won't produce all the right data just with a single call to json_encode(). I would expect you'd need to build a PHP array first from $this->result into the format you want, and json_encode() that - Spudley 2012-04-04 21:01
@Spudley: that's correct, the file-extensions are missing. But they seem to be static, so they also may be appended in javascript later when they are needed - Dr.Molle 2012-04-04 21:03
@Spudley it works perfect, the text comes back with all quotes and everything - Patrioticcow 2012-04-04 21:03
well, ill not use json encode on the links,they don't cause problems because pf the way they are store - Patrioticcow 2012-04-04 21:04
@Patrioticcow - yes, it will encode the object correctly; you'll get a perfect javascript representation of all the object properties; what I meant was your original code is building specific named elements, and adding hard-coded strings to the values; those values won't come out like that if you simply jsonencode the whole object; if you want your finished javascript to look the way you wrote it in the question, you'll need to build a PHP array that matches what you want, and jsonencode that - Spudley 2012-04-04 21:17
Ads