encode|js| Pinyin
http://www.csdn.net/Develop/read_article.asp?id=15043
Unicode and GB conversion libraries with the following addresses
Http://www.blueidea.com/user/qswh/qswhU2GB.js
148k JS, local Test no problem, but put on the internet will feel a bit slow
So need a compact version, collections researches is just GB2312 code, that is, part of the GBK, but enough to
This is particularly streamlined code, but the function is unchanged, the difference is that the version can only be used for common Chinese characters.
What horse with what saddle, also modified the UrlEncode and Getspell algorithm, please do not confuse.
This time the qswhgb2312.js is only 18k and can be used freely on the internet
Unicode and GB conversion libraries and UrlEncode and Getspell functions, please download
Http://www.blueidea.com/user/qswh/qswhGB2312.js
The main code is as follows
var strgb= "Ah ... Snore 齄 ";//gb2312 string, slightly
var qswhspell=[" A ", 0,..," Zuo ", 3747];//phonetic comparison table, slightly
function UrlEncode (str) {
var i,c , p,q,ret= "", strspecial= "!\" #$%& ' () *+,/:;<=>?@[\]^ ' {|} ~%";
for (i=0;i<str.length;i++) {
if (str.charcodeat (i) >=0x4e00) {
var p= Strgb.indexof (Str.charat (i));
if (p>=0) {
q=p%94
p= (P-Q)/94;
ret+= ("%" + (0xb0+p). ToString () + "%" + (0xa1+q). ToString (). toUpperCase ();
}
}
else{
C=str.charat (i);
if (c== "")
ret+= "+";
Else if (Strspecial.indexof (c)!=-1)
ret+= "%" +str.charcodeat (i). ToString (a);
else
ret+=c;
}
return ret;
}
function Getspell (STR,SP) {
var i,c,t,p,ret= "";
if (sp==null) sp= "";
for (i=0;i<str.length;i++) {
if (Str.charcodeat (i) >=0x4e00) {
P=strgb.indexof (Str.charat (i));
if (p>-1&&p<3755) {
for (t=qswhspell.length-1;t>0;t=t-2) if (qswhspell[t]<=p) break;
if (t>0) ret+=qswhspell[t-1]+sp;
}
}
}
Return Ret.substr (0,ret.length-sp.length);
}
The SP is a separator, as the following example
<script src=qswhgb2312.js></script>
<script language=javascript>
document.write (UrlEncode ("Chinese <>\" #%{}|^~[] ' &?+abc ")," <br> ")
document.write (Getspell ("Chinese Programmer's Base"), "<br>"
document.write ("Getspell", "'"), "<br>")
</script>