php javascript url encoding

你说的曾经没有我的故事 提交于 2019-12-29 18:36:21

问题


I have a html text. I had encoded it in php using urlencode function. I want to decode that text in the javascript. when i use unescape function in javascript it replaces all the special characters back but sapce is replaced by '+'. how can i do it correctly so that space is replaced as space itself???


回答1:


Try using rawurlencode instead - urlencode does some things differently for "historical" reasons.

See http://us.php.net/manual/en/function.urlencode.php for more information.




回答2:


PHP rawUrlEncode() == JavaScript encodeURIComponent()

PHP rawUrlDecode() == JavaScript decodeURIComponent()




回答3:


Parenthesis are exceptions to all of what is said in this post.

geek mode on :

false

PHP rawUrlEncode() !== JavaScript encodeURIComponent()

but true

PHP rawUrlEncode() == JavaScript encodeURIComponent()

In other words, there are many special characters that aren't treated as safe in rawurlencode when they are in encodeURIComponent.




回答4:


Try this:

return decodeURIComponent((str + '').replace(/\+/g, '%20'));

Source: http://phpjs.org/functions/urldecode:572



来源:https://stackoverflow.com/questions/1105434/php-javascript-url-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!