views:

46

answers:

2

so here is the case:

i have a string in php (containts 2 words):

windows "mac os"

i encode it with rawurlencode and put it in the URL after ?name= in an anchor. when i click it i see the following in the browsers url.

index.php?name=windows%20%5C%22mac%20os%5C%22

and then i get it with javascript with jquery bbq.

var string = $.deparam.querystring();
document.write(string.name);

and get:

 windows \"mac os\"

i want to get windows "mac os" when i output it with javascript. and then i want to split the words to:

 array[0] = windows
 array[1] = mac os

the first step i think should be to get the string with rawurldecode but for javascript so i will get windows "mac os" rather than windows \"mac os\"?

and then the second step should be what regexp to use in javascript split() to split it to the words in the array above?

ive tried a numerous options but none is working. im stuck here..and its really frustrating. any help with these 2 steps would be appreciated

+1  A: 

The first part of the question:

I tried a rawurlencode on the same input, and it worked fine. My guess is something is adding slashes (perhaps magic_quotes) and you will need to remove them:

So for your link:

<?php $yourstring = 'windows "mac os"'; ?>
<a href="index.php?name=<?php echo rawurlencode(stripslashes($yourstring)) ?>">Click me</a>

That should encode it to look like this:

<a href="index.php?name=windows%20%22mac%20os%22">Click Me</a>

The second part of the question:

I did some searching since I am not a regex expert, and couldn't find anything quick to help you out with splitting the string into an array in javascript, but it looks like it can be done.... probably pretty easily.

Doug Neiner
thanks it worked like a charm:)
weng
+1  A: 

As an addendum to Doug's answer, the way you would do this is:

var words = string.split( " " );
alert( words[ 0 ] ); // windows
alert( words.slice(1).join( " " ) ); // mac os -- Thank you Elijah Grey
Jacob Relkin
`words[1]` would be `mac`, not `mac os`. I think you want to use `words.slice(1).join(" ")`
Eli Grey
actually i need a regular expression to use it on arrays with lots of elements. not just 2 elements as in the example. do you have an idea of how to accomplish this?
weng