admin管理员组文章数量:1202780
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?
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?
Share Improve this question edited Sep 4, 2011 at 15:51 pimvdb 155k80 gold badges311 silver badges356 bronze badges asked Sep 4, 2011 at 15:43 Sandro AntonucciSandro Antonucci 1,7538 gold badges34 silver badges60 bronze badges4 Answers
Reset to default 20Use rawurlencode
instead of urlencode
in PHP.
Follow this link at php's own documention rawurlencode
rawurlencode
will do the trick, the link is for reference.
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
I was having the same problem between rawurlencode()
and encodeURIComponent()
. The difference for me was that I didn't discover the issue until using encodeURIComponent()
in numerous source files, so going back to fix and change them all and then re-test everything was not an option.
Fortunately, JS gives you the ability to "hijack" built-in functions by assigning the same name to a new function. You can thus change the behavior of encodeURIComponent()
with a very slight modification to Phantom1412's code, and without having to recode anything.
Just put this script in your page before your code makes any calls to encodeURIComponent()
:
var encodeURIComponentOld = encodeURIComponent;
encodeURIComponent = function(str) {
return encodeURIComponentOld(str).replace(/[!'()*]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16);
});
};
本文标签: JavaScript encodeURIComponent vs PHP ulrencodeStack Overflow
版权声明:本文标题:JavaScript encodeURIComponent vs PHP ulrencode - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738575771a2100869.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论