Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which JavaScript function is compatible with PHP urldecode function?

Tags:

javascript

php

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:

  • escape()
  • encodeURI()
  • encodeURIComponent()
like image 748
Tod Avatar asked Nov 14 '22 01:11

Tod


1 Answers

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.

like image 150
poke Avatar answered Dec 24 '22 13:12

poke