赞
踩
格式一:&#XXXX;&#XXXX;
示例:您好
结果:您好
格式二:\uXXXX\uXXXX
示例:\u60a8\u597d
结果:您好
格式三:&#DDDDD;&#DDDDD;
示例:您好
结果:您好
- //convtype为转换类型,转换类型为我上文中提到的4中,code为需要转换的字符串
- function myconf(convtype,code){
- switch(convtype){
- case "totype1":
- return ascii(code);
- case "totype2":
- return unicode(code);
- case "totype3":
- return unicode1(code);
- case "tohanzi":
- return reconvert(code);
- }
- }
- function ascii(str){
- var value='';
- for (var i = 0; i < str.length; i++)
- value += '\&#x' + parseInt(str.charCodeAt(i)).toString(16)+';';
- return value;
- }
- function unicode(str){
- var value='';
- for (var i = 0; i < str.length; i++)
- value += '\\u' + parseInt(str.charCodeAt(i)).toString(16);
- return value;
- }
- function unicode1(str){
- var value='';
- for (var i = 0; i < str.length; i++)
- value += '&#' + str.charCodeAt(i) + ';';
- return value;
- }
- function reconvert(str){
- str = str.replace(/(\\u)(\w{1,4})/gi,function($0){
- return (String.fromCharCode(parseInt((escape($0).replace(/(%5Cu)(\w{1,4})/g,"$2")),16)));
- });
- str = str.replace(/(&#x)(\w{1,4});/gi,function($0){
- return String.fromCharCode(parseInt(escape($0).replace(/(%26%23x)(\w{1,4})(%3B)/g,"$2"),16));
- });
- str = str.replace(/(&#)(\d{1,6});/gi,function($0){
- return String.fromCharCode(parseInt(escape($0).replace(/(%26%23)(\d{1,6})(%3B)/g,"$2")));
- });
- return str;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。