Forum Moderators: open

Message Too Old, No Replies

Trying to use " or , in an array

then use split(","), I need to escape them

         

Br3nn4n

9:29 am on Jun 18, 2008 (gmt 0)

10+ Year Member



I'm working with an array in Javascript:

slide[0] = "image1,image1.jpg,350,262,Young Wild Things tour 2007 featuring Cute Is What We Aim For and other popular bands,William Badarak-The North Star";
slide[1] = "image2,image2.jpg,273,350,North one acts a scene from Cassie Geists performance entitled Bus Stop,Jacob Gray-The North Star";

For element number 4 (counting from 0 up), I really need to be able to use characters such as quotes (") and commas (,) but my code does this later:

var strArray = slide[i].split(",");

to get the data apart.

I've tried escape() but I don't think that's right. How can I do this? Also, if I include quotes (") it closes up the array and messes everything up.

Ideas?

Thanks so much!

lavazza

9:40 am on Jun 18, 2008 (gmt 0)

10+ Year Member



use single quotes to declare your array elements

e.g.

slide[0] = 'image1,image1.jpg,350,262,Young Wild Things tour 2007 featuring Cute Is What We Aim For and other popular bands,William Badarak-The North Star';

If there's an apostrophe in the string, escape it

e.g.
A Dog's Dinner
A Dog\'s Dinner


For the commas, maybe you could use HTML 4.0 Latin-1 entities equivalents as a workaround

‚ OR ‚ OR ‚

httpwebwitch

8:12 pm on Jun 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



or if possible build your arrays differently to begin with. Assign each element separately.

slide[1] = new Array()
slide[1][0] = "image2";
slide[1][1] = "image2.jpg";
slide[1][2] = "273";
slide[1][3] = "350";
slide[1][4] = "it's got \"quotes\" in it, too";
slide[1][5] = "more, more, more";

then you don't need to worry about using split() to parse array elements later

rocknbil

8:40 pm on Jun 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A Dog\'s Dinner

Reverse engineering this solution, another way to do it: escape any quotes when putting them in the initial array.

item[0] = "yeah, he said \"wow.\"";

Br3nn4n

4:15 am on Jun 19, 2008 (gmt 0)

10+ Year Member



Ahh geez, you gotta be kidding me. The standard escape \ works and I didn't even think to try it? Hahahaha

i'm ultimately trying to make a php script put together a .js file from some form data.

I suppose using httpwebwitch's solution and having a finite number of pieces of data to enter for each array element I could do that rather easily actually.

For example, do something like this:

slide[0] = new Array();
slide[0][0] = "<?php echo $description; ?>";
slide[0][1] = "<?php echo $filename; ?>";
slide[0][2] = "<?php echo $width; ?>";

...and so on. Side question kinda...would that work?

httpwebwitch

5:14 am on Jun 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



yes

PHP has a built-in function for escaping quotes, which will no doubt be useful.

and if I'm not mistaken, there are PHP libraries that turn a complex PHP array into JSON, which may save you some heavy looping