views:

100

answers:

1

I've a Zend Framework URI like /controller/action/var1/value1/var2/value2 .

  1. Value2 has a space character in it. How should I encode it in PHP? When I use urlencode with value2, this converts space into '+' instead of '%20f'. Is that ok?

  2. This value2 is also added to a href location by javascript on client side. I'm using escape function there but when I click link I neither see '+' nor '%20f' in firefox address bar. Though when I see it in firebug 'net' tab, I see %20f.

Which functions should I use in PHP and javascript?

+2  A: 

About your first question, that is the difference between urlencode and rawurlencode :

var_dump(urlencode("hello, world"));

Will get you :

string 'hello%2C+world' (length=14)

While

var_dump(rawurlencode("hello, world"));

will get you :

string 'hello%2C%20world' (length=16)

I suppose both should be OK ; but feel free to give it a try, just in case ;-)


About the second point : Firefox tries to make URLs "prettier", displaying them in a human-readable way, instead of encoded -- which is bad for us developpers, but nice to end-users.

For instance, if I type this URL in Firefox's address bar :

http://tests/temp/temp.php?a=hello%2C%20world

When I press the enter key, it's automatically translated to

http://tests/temp/temp.php?a=hello%2C%20world

If it works the way you are doing it (and, as you are seeing an encoded URL with Firebug, it's probably working), everything's OK ;-)

Pascal MARTIN
I believe, though I'm not sure, that `%20` is more correct than `+` in the path portion of URLs. Javascript's `escape()` function will give you very similar behavior to PHP's `rawurlencode()`, so I'd recommend using that on the JS side.
pix0r
@pixor : for the sake of consistency, you are probably right :-)
Pascal MARTIN