Zend Framework URI space encoding in PHP and Javascript
-
21-09-2019 - |
문제
I've a Zend Framework URI like /controller/action/var1/value1/var2/value2 .
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?
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?
해결책
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 ;-)