Domanda

Since the escape function is deprecated, I created this function as it is used throughout my website and it would be tedious to replace every occurence.

function escape(text) {
    text = text.replace(/'/g, "%27");
    return encodeURI(text);
}

This changes a string such as This is Ben's bookcase into This%20is%20Ben%2527s%20Bookcase.

When I try to decode this in PHP, using rawurldecode(), I end up with this This is Ben%27s Bookcase.

How can I make urldecode in PHP decode single ticks?

È stato utile?

Soluzione

You are doing it wrong.

The original character ' is turned into %27, and then the percent sign is turned into %25. This second step must not happen.

If you want to encode everything else, and then also single quotes, first call encodeURI, then replace.

That way, ' will return unaltered from escaping, and then be transformed into %27, with no further changes.

PHP can then decode the string properly.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top