PDA

View Full Version : Problem with Ext.urlDecode()



ethraza
17 Jun 2007, 10:32 AM
I tryed to use urlDecode() to decode a string passed from PHP and encoded with urlencode(), but what I saw in Firebug was "malformed URI sequence".
I searched on Internet and seed that urlencode() is not compatible with decodeURIComponent used in urlDecode().
I noticed too that urlDecode() don't take out the + that come in place of spaces.

I changer the urlDecode like this to get it work with PHP urlencode:


function urlDecode(string, overwrite){
if(!string || !string.length){
return {};
}
var obj = {};
var pairs = string.split('&');
var pair, name, value;
var lsregexp = /\+/g;
for(var i = 0, len = pairs.length; i < len; i++){
pair = pairs[i].split('=');
name = unescape(pair[0]);
value = unescape(pair[1]).replace(lsregexp, " ");
if(overwrite !== true){
if(typeof obj[name] == "undefined"){
obj[name] = value;
}else if(typeof obj[name] == "string"){
obj[name] = [obj[name]];
obj[name].push(value);
}else{
obj[name].push(value);
}
}else{
obj[name] = value;
}
}
return obj;
}
I changed decodeURIComponent for unescape and added I regexp to replace + for " " (spaces).
Now urlDecode() is working with urlencode() from PHP. ;)

I spect that it helps!