0

Friends, I am currently using the below function to encode my data and send them through GET method... I am using ajax to send and php to receive them...

function urlencode(a){
     a=encodeURIComponent(a);
     a=a.replace(/\\/g,'%5C');
     a=a.replace(/!/g,'%21');
     a=a.replace(/'/g,'%27');
     a=a.replace(/\(/g,'%28');
     a=a.replace(/\)/g,'%29');
     a=a.replace(/\*/g,'%2A');
     a=a.replace(/~/g,'%7E');
     a=a.replace(/%20/g,'+');
     a=a.replace(/%26amp%3B/gi,'%26');
     a=a.replace(/%26lt%3B/gi,'%3C');
     a=a.replace(/%26gt%3B/gi,'%3E');
     a=a.replace(/%26nbsp%3B/gi,'%C2%A0');
     return a;
}

after passing the string through encodeURIComponent(), I used extra replace() method so that the function encode the string for my link as similar as php...

Now my question is do anyone know any shorter or easy alternative way to do the same thing...

I place this question only for learning purpose... no jQuery please...

Naz
  • 2,520
  • 2
  • 16
  • 23
  • I don't understand why a down vote for this question...?? – Naz Nov 25 '12 at 13:54
  • I didn't downvote it, but the point of what you are doing seems really questionable. (Edit: ahh, I see now) – Pekka Nov 25 '12 at 13:57
  • @Pekka I didn't point at you... to him who did that... costed his free first down vote in my question... – Naz Nov 25 '12 at 13:58
  • Always, always use built-in methods to escape things. Never roll your own. – tucuxi Nov 25 '12 at 15:27

2 Answers2

0

Using escape() instead of encodeURIComponent will eliminate at least some of the replace()'s you're using. E.g:

function urlencode(a){
 return escape(a);
   .replace(/\*/g,'%2A');
   .replace(/%20/g,'+');
   .replace(/%26amp%3B/gi,'%26');
   .replace(/%26lt%3B/gi,'%3C');
   .replace(/%26gt%3B/gi,'%3E');
   .replace(/%26nbsp%3B/gi,'%C2%A0');
}
broofa
  • 37,461
  • 11
  • 73
  • 73
0

If you are sending GET requests from JS to PHP, you should use

  • encodeURIComponent(), for each parameter, on the javascript side
  • nothing on the PHP side (because $_GET is already urldecode()d; read the notes section in its linked documentation)

If you have to url-encode things in PHP, use urlencode(), which is guaranteed to be compatible with the corresponding urldecode().

If you want to learn how to do it "by yourself", read the source-code in the corresponding libraries to make sure you do not miss any scenario... it is generally a bad idea to re-implement library functions.

tucuxi
  • 17,561
  • 2
  • 43
  • 74
  • read the question well very before answering... you don't understand my question at all... second I posted `javascript` code not `php`... – Naz Nov 25 '12 at 15:52
  • Write the question very well before asking, and read my answer carefully. There is no need to do what you are doing in your question, if your purpose is to send a GET request. A call to `encodeURIComponent()` (which is standard JS, not PHP) is enough to encode each component. On the server side, you do not require any PHP at all to decode it (you write /encode the string for my link as similar to PHP/ in your question). – tucuxi Nov 25 '12 at 15:56