PHP的URLEncode和URLDecode函数用Javascript实现

PHP的URLEncode和URLDecode函数用Javascript实现
很多朋友说,Javascript的得到功能也可以实现,但是有所有的bug。下面看看下面的函数。测试没有什么问题。我在以前的文章中说,这是不是作为PHP urldecode的功能相同。以下是我改写据高个人代码URLDecode函数的Javascript版本。在参考链接不是在文章的开头列举了。就像以前URLEncode函数,只有UTF-8版本实现。

1、URLEncode

使用方法:URLEncode(STR);
函数的URLEncode(clearstring)
{
输出=;
变量x=0;

clearstring = utf16to8(clearstring.tostring());
VaR的正则表达式= /(^ {就- _。} *)/;

而(x<clearstring。长度)
{
VaR的比赛= regex.exec(clearstring.substr(x));
如果(匹配)!= null match.length > 1场{ 1 }!=)
{
输出=匹配{ 1 };
x =匹配{ 1 }。长度;
}
其他的
{
如果(clearstring {X} = =)
输出++;
其他的
{
var(x)= clearstring.charcodeat字符码;
无功hexval = charcode.tostring(16);
输出+ =‘%'+(hexval.length < 2尺':')+ hexval.touppercase();
}
++;
}
}

功能utf16to8(STR)
{
var,i,莱恩,c;

out;
str.length len =;
为(i = 0;i;;i;+)
{
C = str.charcodeat(我);
如果((C =端口(C)< = 0x007f))
{
= str.charat(我);
}
如果(C > 0x07ff)
{
= String.fromCharCode(0xE0(|(C > 12)0x0f));
= String.fromCharCode(0x80(|(C > 6)0x3f));
= String.fromCharCode(0x80(|(C > 0)0x3f));
}
其他的
{
= String.fromCharCode(0xC0(|(C > 6)0x1F));
= String.fromCharCode(0x80(|(C > 0)0x3f));
}
}
回来了;
}

返回输出;
}
2、URLDecode

使用方法:URLDecode(URL);
函数的URLDecode(encodedstring)
{
无功输出= encodedstring;
VaR值binVal,thisstring;
无功myregexp = /(% { ^ % } { 2 }) /;
功能utf8to16(STR)
{
var,i,莱恩,c;
VaR的位置,char3;

out;
str.length len =;
我= 0;
而(我)
{
C = str.charcodeat(我+ +);
开关(c > 4)
{
案例0:病例1:病例2:病例3:病例4:病例5:
= str.charat(i-1);
打破;
案例12:案例13:
- = str.charcodeat(我+ +);
String.fromCharCode(((C = 0x1F)<6)|(位置0x3F));
打破;
案例14:
- = str.charcodeat(我+ +);
char3 = str.charcodeat(我+ +);
String.fromCharCode(C =(0x0f)(12)|
((- | 0x3f)<6)
((char3 0x3f)<< 0)))
打破;
}
}
回来了;
}
而((赛= myregexp.exec(输出))!= null
match.length > 1
匹配{ 1 }!=)
{
binVal = parseInt(匹配{ 1 }。substr(1),16);
thisstring = String.fromCharCode(binVal);
输出= output.replace(匹配{ 1 },thisstring);
}

/ /输出= utf8to16(输出);
输出= output.replace( / +/g,);
utf8to16输出=(输出);
Return output;
}
当服务器端转码通过PHP urlencode,它可以利用JS URLDecode解析。

tag:函数电脑软件URLEncodephpJavascript

相关内容