Javascript 我如何取消转义字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5893965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I unescape a string?
提问by user737767
using ajax get method to pass a variable. where i use to escape the string with escape() function.
使用ajax get 方法传递一个变量。我用escape()函数来转义字符串。
In php, I receive the value and insert into a database.
在 php 中,我接收该值并插入到数据库中。
How do I unescape the string?
我如何取消转义字符串?
For example, for:
例如,对于:
balaji's
I get
我得到
balaji%27s.
回答by Babailiica
You can use this function instead of urldecode()
您可以使用此函数代替 urldecode()
function utf8_urldecode($str) {
return html_entity_decode(preg_replace("/%u([0-9a-f]{3,4})/i", "&#x\1;", urldecode($str)), null, 'UTF-8');
}
回答by Shweta Khattar
You can try this:
你可以试试这个:
htmlspecialchars(urldecode($str));
回答by T.J. Crowder
Rather than using escape
on the client-side, use encodeURIComponent
. escape
does notdo URI-encoding. (It does something similar, but different enough that you'll have trouble.) encodeURIComponent
does standard URI-encoding. PHP should decode it for you automatically if you're sending the data in the normal way, e.g.:
而不是escape
在客户端使用,使用encodeURIComponent
. escape
并没有做URI编码。(它做一些类似的事情,但足够不同,你会遇到麻烦。)encodeURIComponent
做标准的 URI 编码。如果您以正常方式发送数据,PHP 应该自动为您解码,例如:
var word = "balaji's";
$.ajax({
url: "yoururl",
data: encodeURIComponent("word") + "=" + encodeURIComponent(word)
});
If you're not sending the data in the default "multipart form" form (e.g., you're overriding dataType
), you'll have to decode it yourself via urldecode
.
如果您没有以默认的“多部分形式”形式发送数据(例如,您要覆盖dataType
),则您必须通过urldecode
.
回答by jedwards
Consider the PHP functions, urldecode()or rawurldecode().
考虑 PHP 函数urldecode()或rawurldecode()。
One difference, it seems, is in the handling of + (plus signs).
一个区别似乎在于 +(加号)的处理。
You should make sure to test with strings containing plus signs to ensure you have chosen the correct function.
您应该确保使用包含加号的字符串进行测试,以确保您选择了正确的函数。