O
ottodv

  • Nov 2, 2008
  • Joined Nov 2, 2008
  • In addition to my previous post I think it would be good to point out that there is also the encodeURIComponent() function in JavaScript that is probably suitable for the purposes intended in this thread. It doesn't escape: ! ~ * ' ( ) like the urlencode PHP function does, but I have experienced no problems passing a argument encoded by encodeURIComponent to a PHP script. Another plus is that encodeURIComponent encodes all UTF-8 characters while escape encodes ISO Latin characters.

    • The solutions above only replaces the first occurrence of each pattern. It's better to use a global replace:

      function urlencode(str) {
      return escape(str).replace(/+/g,'%2B').replace(/%20/g, '+').replace(/*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
      }