简体   繁体   English

php javascript url 编码

[英]php javascript url encoding

I have a html text.我有一个 html 文本。 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 '+'.我在 php 中使用 urlencode function 对其进行了编码。我想解码 javascript 中的文本。当我在 javascript 中使用 unescape function 时,它会替换所有特殊字符,但 sapce 被“+”替换。 how can i do it correctly so that space is replaced as space itself???我怎样才能正确地做到这一点,以便将空间替换为空间本身???

PHP rawUrlEncode() == JavaScript encodeURIComponent() PHP rawUrlEncode()== JavaScript encodeURIComponent()

PHP rawUrlDecode() == JavaScript decodeURIComponent() PHP rawUrlDecode()== JavaScript decodeURIComponent()

Try using rawurlencode instead - urlencode does some things differently for "historical" reasons. 尝试使用rawurlencode - urlencode因“历史”原因而做了一些不同的事情。

See http://us.php.net/manual/en/function.urlencode.php for more information. 有关更多信息,请参见http://us.php.net/manual/en/function.urlencode.php

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. 换句话说,当rawurlencode位于encodeURIComponent时,有许多特殊字符在rawurlencode中被视为不安全。

Try this: 试试这个:

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

Source: http://phpjs.org/functions/urldecode:572 资料来源: http//phpjs.org/functions/urldecode : 572

PHP rawUrlEncode with JS unescape (deprecated but working) PHP rawUrlEncode with JS unescape (已弃用但有效)

For me decodeURIComponent throw errors sometimes.对我来说, decodeURIComponent有时会抛出错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM