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%27s.

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.