JavaScript encodeURIComponent vs PHP ulrencode

喜欢而已 提交于 2019-12-07 10:29:42

问题


The difference between them is that the PHP's urlencode encodes spaces with + instead of %20?

What are the functions that do the same thing for both the languages?


回答1:


Use rawurlencode instead of urlencode in PHP.




回答2:


Follow this link at php's own documention rawurlencode

rawurlencode will do the trick, the link is for reference.




回答3:


Actually even with JavaScript encodeURIComponent and PHP rawurlencode, they are not exactly the same too, for instance the '(' character, JavaScript encodeURIComponent will not convert it however PHP rawurlencode will convert it to %28. After some experiments and tips from others such as this question another Stackoverflow question.

I found the ultimate solution here.

All you need to do is use the add following code

function fixedEncodeURIComponent(str) {
    return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
        return '%' + c.charCodeAt(0).toString(16);
    });
}

They will be EXACTLY the same now, for example

fixedEncodeURIComponent(yourUrl) (JavaScript) = (PHP) rawurlencode(yourUrl)

no problem with decode, you can use decodeURIComponent() for JavaScript and rawurldecode for PHP



来源:https://stackoverflow.com/questions/7300328/javascript-encodeuricomponent-vs-php-ulrencode

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