vote up 2 vote down star

Suppose that I have a string $var:

//php code

$var = "hello,world,test";
$exp = explode(",",$var);

Now I get the array as exp[0],exp[1],exp[1] as 'hello', 'world' and 'test', respectively.

I want to use this all value in javascript in this:

var name = ['hello','world','test'];

How can I generate that JavaScript in PHP?

flag

34% accept rate
Why on earth are you first loading values into vars in php and then overload them to javascript anyway? – Younes Jan 21 at 13:37
var name = ($exp[0],$exp[1],$exp[2]); to be honest i don't know what you are asking, but this is what you do in your examples... – Younes Jan 21 at 13:38
1  
@Younes: It is helpful, sometimes... If you wan't to show, for example, a variable that is stored in a session with Javascript without firing a ajax request after the page is loaded. – Harmen Jan 21 at 13:41
@Harmen: Okay, well thanks for taking the time to explain :)! – Younes Jan 21 at 14:01
@vipinsahu - could you take a moment to review Karl's answer, and accept it if it works for you - his is better than mine! – Dominic Rodger Jan 21 at 14:56

2 Answers

vote up 1 vote down check

Karl B's answer is better - use that!

Wouldn't an easier way be like this:

$var = "hello,world,test";
$var = str_replace(",", "','", $var);

Then wherever you're spitting out JavaScript (assuming you can use PHP there):

var name = ['<?php echo $var; ?>'];

This doesn't deal properly with quoted values though - if you want that, you're better off with using fgetscsv et al.

If you're determined to use explode, then you can use its other-half, implode like this in your output:

var name = ['<? php echo implode("','", $var); ?>'];
link|flag
Thanks a lot Dominic Rodger i got the concept thanks – vipinsahu Jan 21 at 13:43
You should remember to escape quotes. If $var = "he'llo,world" then above code will fail. Use array_walk($var, 'addslashes'); before explode. – Crozin Jan 21 at 13:44
String declarations in JavaScript are not the same as in PHP. – Gumbo Jan 21 at 14:28
vote up 9 vote down

I would have thought json_encode would be the most reliable and simplest way.

E.g.

$var = "hello,world,test";
$exp = explode(",",$var);
print json_encode($exp);
link|flag
+1 This the the best way to turn any PHP value into a JavaScript value literal. Remember to use at least the JSON_HEX_TAG flag if you are in a <script> block to avoid </ in the string breaking the script block, or JSON_HEX_TAG|JSON_HEX_AMP for XHTML compatibility. – bobince Jan 21 at 13:50
+1 from me too - didn't know about json_encode - can I suggest adding some sample code that the OP can use? Hopefully he'll then accept your answer instead, and I can delete mine. – Dominic Rodger Jan 21 at 13:55
1  
Have added a bit of example code as recommended (and as I should have done in the first place). – Karl B Jan 21 at 14:24
Great, thanks Karl! – Dominic Rodger Jan 21 at 14:48
Thanks Karl its really cool – vipinsahu Jan 21 at 15:32

Your Answer

Get an OpenID
or
never shown

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