Which JavaScript function is compatible with PHP urldecode function?
On the server side the PHP code will be using urldecode()
function to decode, but the JavaScript code is responsible to encode the URL. Which of the following JavaScript function is compatible with the PHP urldecode()
function:
You can use either encodeURI
or encodeURIComponent
. The php manual states:
Decodes any %## encoding in the given string.
So whatever the encoding function encodes, all %##
sequences are decoded. So you can use either one of the JavaScript functions to encode it.
edit:
(In kind-of response to Gumbo's answer, which he removed?)
php's urldecode
also decodes +
signs to spaces (because it implements a different standard). To make sure that no plus signs that are actually intended are decoded on the php side, just use encodeURIComponent
to be sure. That encodes +
to %2B
, which is then again safe from php's urldecode
.