How do I unescape a string?
using ajax get method to pass a variable. where i use to escape the string with escape() function.
In php, I receive the value and insert into a database.
How do I unescape the string?
For example, for:
balaji's
I get
balaji%2开发者_如何学编程7s.
You can use this function instead of 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');
}
You can try this:
htmlspecialchars(urldecode($str));
Rather than using escape
on the client-side, use encodeURIComponent
. escape
does not do 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.:
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
.
Consider the PHP functions, urldecode() or 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.
as a last resort you can try something like this (this list is not exhaustive, make your own or complete it)
$search = ["%20",'%3C','%3E','%3D','%22','%3A','%3B','%E0','%E9','%E8','%26','%E2', '%u2019'];
$replace = [' ', '<', '>', '=','"',':',';','à','é','è','&','â', '\''];
str_replace($search, $replace, $your_str);
精彩评论