lnzq5lnzqw

今天分享一个日常开发中可能用到的小功能。简单来说,汉字可以转换成拼音首字母。当然,我写不出这样的函数。源码来自其他民间大神的分享。博主把功能演示录在这里,方便以后重用,也方便每个需要这个功能的人。

按如下方式输入姓名张三。

lnzq5lnzqw

点击按钮获得汉语拼音的第一个字母。

博主整理了代码的可读性,下面直接列出代码。

& lt!doctype html & gt

& lthtml & gt

& lthead & gt

& ltmeta charset = & # 34UTF-8 & # 34;& gt

& lttitle & gt& lt/title & gt;

& ltmeta name = & # 34视窗& # 34;内容= & # 34;宽度=设备宽度,初始比例=1,最小比例=1,最大比例=1,用户可扩展=否& # 34;/& gt;

& lt/head & gt;

& ltbody & gt

& lt输入类型= & # 34;正文& # 34;id = & # 34正文& # 34;& gt

& lt按钮onclick = & # 34makePy()& # 34;& gt得到第一个字母

& ltp id = & # 34text _ value & # 34& gt& lt/p & gt;

& lt脚本类型= & # 34;文本/JaScript & # 34;& gt

//汉字拼音首字母列表此列表包含20902个汉字,用于匹配ToChineseSpell。

//使用函数。此表中包含的字符的Unicode编码范围是19968到40869,由XDesigner编译。

var strChineseFirstPY = & # 34ydyqsxmwzsxymgcczqpssqbycdscdqldylybssjgyzzjjfkcclzdhwdwzjljpfyynwjtmyhzwzhlzppqhgscyyynjqyxgjhsdsjnjkktmomlcrxypsnqsecqzgglyjljlyzzsecyyyyyyyhqwjs sggyxyzyjwwkdjychmyxjtlxjyqbyxqzldwrdjrwysrldzjpcbzrcftleczzxf zxljfrmyzflxysqxxjkxrmqdzmmyybsqbhgzmwfwxgmxlzpyytgzyccdcxyzxywgsyjyznhpzjsqsyxsxrtfyzgrhztszztchcbfclsyxzlzqmzlmplmxzjflbyzmyqhjsxrxrxrxzzzsslyfrczj rcrxhhzxqydyhxsjhzczxzxzztynsxqlpxzqzqqpymlxzkjxlxcjlxcysxzzlz ;

//这里有375个多音字,数据来自http://www.51window.net/page/pinyin.

var oMultiDiff = { & # 3419969":"DZ & # 34, "19975":"WM & # 34, "19988":"QJ & # 34;, "20048":"YL & # 34;, "20056":"SC & # 34, "20060":"NM & # 34, "20094":"QG & # 34;, "20127":"QJ & # 34;, "20167":"QC & # 34, "20193":"YG & # 34;, "20250":"KH & # 34, "20256":"ZC & # 34;, "20282":"SC & # 34, "20285":"QJG & # 34, "20291":"TD & # 34, "20314":"YD & # 34, "20340":"NE & # 34, "20375":"TD & # 34, "20389":"YJ & # 34;, "20391":"CZ & # 34, "20415":"PB & # 34, "20446":"YS & # 34;, "20447":"SQ & # 34, "20504":"TC & # 34, "20608":"KG & # 34, "20854":"QJ & # 34;, "20857":"ZC & # 34;, "20911":"PF & # 34, "20504":"TC & # 34, "20608":"KG & # 34, "20854":"QJ & # 34;, "20857":"ZC & # 34;, "20911":"PF & # 34, "20985":"AW & # 34, "21032":"PB & # 34, "21048":"XQ & # 34;, "21049":"SC & # 34, "21089":"YS & # 34;, "21119":"JC & # 34, "21242":" & # 34, "21273":"SC & # 34, "21305":"YP & # 34;, "21306":"QO & # 34;, "21330":"ZC & # 34;, "21333":"SDC & # 34;, "21345":"QK & # 34;, "21378":"CA & # 34, "21397":"SC & # 34, "21414":"XS & # 34;, "21442":"SC & # 34, "21477":"JG & # 34;, "21480":"TD & # 34, "21484":"ZS & # 34;, "21494":"YX & # 34, "21505":"YX & # 34, "21512":"HG & # 34, "21523":"XH & # 34;, "21537":"PB & # 34, "21542":"PF & # 34, "21549":"KH & # 34, "21571":"E & # 34, "21574":"达& # 34;, "21588":"TD & # 34, "21589":"O & # 34, "21618":"ZC & # 34;, "21621":"KHA & # 34;, "21632":"ZJ & # 34;, "21654":"KG & # 34, "21679":"LKG & # 34;, "21683":"KH & # 34, "21710":"一& # 34;, "21719":"YH & # 34;, "21734":"悲哀& # 34;, "21769":"一& # 34;, "21780":"WN & # 34;, "21804":"XH & # 34;, "21834":"一& # 34;, "21899":"ZD & # 34;, "21903":"RN & # 34, "21908":"沃& # 34;, "21939":"ZC & # 34;, "21956":"萨& # 34;, "21964":"丫& # 34;, "21970":"TD & # 34, "22003":"一& # 34;, "22031":"JG & # 34;, "22040":"XS & # 34;, "22060":"ZC & # 34;, "22066":"ZC & # 34;, "22079":"MH & # 34, "22129":"XJ & # 34;, "22179":"XA & # 34, "22237":"NJ & # 34, "22244":"TD & # 34, "22280":"JQ & # 34;, "22300":"YH & # 34;, "22313":"XW & # 34;, "22331":"YQ & # 34, "22343":"YJ & # 34;, "22351":"PH & # 34, "22395":"DC & # 34;, "22412":"TD & # 34, "22484":"PB & # 34, "22500":"PB & # 34, "22534":"ZD & # 34;, "22549":"DH & # 34, "22561":"PB & # 34, "22612":"TD & # 34, "22771":"KQ & # 34;, "22831":"HB & # 34, "22841":"JG & # 34;, "22855":"QJ & # 34;, "22865":"XQ & # 34;, "23013":"ML & # 34, "23081":"WM & # 34, "23487":"SX & # 34;, "23558":"QJ & # 34;, "23561":"YW & # 34;, "23586":"YW & # 34;, "23614":"YW & # 34;, "23615":"SN & # 34, "23631":"PB & # 34, "23646":"ZS & # 34;, "23663":"ZT & # 34;, "23673":"YG & # 34;, "23762":"TD & # 34, "23769":"ZS & # 34;, "23780":"QJ & # 34;, "23884":"QK & # 34;, "24055":"XH & # 34;, "24113":"DC & # 34;, "24162":"ZC & # 34;, "24191":"嘎& # 34;, "24273":"QJ & # 34;, "24324":"NL & # 34, "24377":"TD & # 34, "24378":"QJ & # 34;, "24439":"PF & # 34, "24554":"ZS & # 34;, "24683":"TD & # 34, "24694":"我们& # 34;, "24733":"LK & # 34, "24925":"TN & # 34, "25094":"ZG & # 34;, "25100":"XQ & # 34;, "25103":"XH & # 34;, "25153":"PB & # 34, "25170":"PB & # 34, "25179":"KG & # 34, "25203":"PB & # 34, "25240":"ZS & # 34;, "25282":"FB & # 34, "25303":"纳& # 34;, "25324":"KG & # 34, "25341":"ZY & # 34;, "25373":"WZ & # 34;, "25375":"XJ & # 34;, "25384":"一& # 34;, "25457":"一& # 34;, "25528":"SD & # 34, "25530":"SC & # 34, "25552":"TD & # 34, "25774":"ZC & # 34;, "25874":"ZC & # 34;, "26044":"YW & # 34;, "26080":"WM & # 34, "26292":"PB & # 34, "26333":"PB & # 34, "26355":"ZY & # 34;, "26366":"CZ & # 34, "26397":"ZC & # 34;, "26399":"QJ & # 34;, "26415":"ZS & # 34;, "26451":" & # 34, "26526":"ZC & # 34;, "26552":"JG & # 34;, "26561":"TD & # 34, "26588":"JG & # 34;, "26597":"CZ & # 34, "26629":"ZS & # 34;, "26638":"YL & # 34;, "26646":"XQ & # 34;, "26653":"KG & # 34, "26657":"XJ & # 34;, "26727":"HG & # 34, "26894":"ZC & # 34;, "26937":"ZS & # 34;, "26946":"ZC & # 34;, "26999":"KJ & # 34, "27099":"KJ & # 34, "27449":"YQ & # 34, "27481":"XS & # 34;, "27542":"ZS & # 34;, "27663":"ZS & # 34;, "27748":"TS & # 34, "27784":"SC & # 34, "27788":"ZD & # 34;, "27795":"TD & # 34, "27812":"O & # 34, "27850":"PB & # 34, "27852":"MB & # 34, "27895":"SL & # 34, "27898":"PL & # 34, "27973":"QJ & # 34;, "27981":"KH & # 34, "27986":"HX & # 34;, "27994":"XJ & # 34;, "28044":"YC & # 34;, "28065":"WG & # 34, "28177":" & # 34, "28267":"QJ & # 34;, "28291":"KH & # 34, "28337":"ZQ & # 34;, "28463":"TL & # 34, "28548":"DC & # 34;, "28601":"TD & # 34, "28689":"PB & # 34, "28805":"JG & # 34;, "28820":"QG & # 34;, "28846":"PB & # 34, "28952":"TD & # 34, "28975":"ZC & # 34;, "29100":"一& # 34;, "29325":"QJ & # 34;, "29575":"SL & # 34, "29602":"FB & # 34, "30010":"TD & # 34, "30044":"CX & # 34;, "30058":"PF & # 34, "30091":"YSP & # 34;, "30111":"YN & # 34;, "30229":"XJ & # 34;, "30427":"SC & # 34, "30465":"SX & # 34;, "30631":"YQ & # 34, "30655":"QJ & # 34;, "30684":"QJG & # 34, "30707":"SD & # 34, "30729":"XH & # 34;, "30796":"LG & # 34, "30917":"PB & # 34, "31074":"NM & # 34, "31085":"JZ & # 34;, "31109":"SC & # 34, "31181":"ZC & # 34;, "31192":"MLB & # 34;, "31293":"JQ & # 34;, "31400":"YX & # 34, "31584":"YJ & # 34;, "31896":"ZN & # 34, "31909":"ZY & # 34;, "31995":"XJ & # 34;, "32321":"PF & # 34, "32327":"ZY & # 34;, "32418":"HG & # 34, "32420":"XQ & # 34;, "32421":"HG & # 34, "32438":"LG & # 34, "32473":"GJ & # 34;, "32488":"TD & # 34, "32521":"QJ & # 34;, "32527":"PB & # 34, "32562":"ZSQ & # 34, "32564":"JZ & # 34;, "32735":"ZD & # 34;, "32793":"PB & # 34, "33071":"PF & # 34, "33098":"XL & # 34, "33100":"丫& # 34;, "33152":"PB & # 34, "33261":"CX & # 34;, "33324":"BP & # 34, "33333":"TD & # 34, "33406":"丫& # 34;, "33426":"WM & # 34, "33432":"PB & # 34, "33445":"JG & # 34;, "33486":"ZN & # 34, "33493":"TS & # 34, "33507":"QJ & # 34;, "33540":"QJ & # 34;, "33544":"ZC & # 34;, "33564":"XQ & # 34;, "33617":"YT & # 34, "33632":"QJ & # 34;, "33636":"XH & # 34;, "33637":"YX & # 34, "33694":"WG & # 34, "33705":"PF & # 34, "33728":"YW & # 34;, "33882":"SR & # 34, "34067":"WM & # 34, "34074":"YW & # 34;, "34121":"QJ & # 34;, "34255":"ZC & # 34;, "34259":"XL & # 34, "34425":"JH & # 34;, "34430":"XH & # 34;, "34485":"KH & # 34, "34503":"YS & # 34;, "34532":"HG & # 34, "34552":"XS & # 34;, "34558":"叶& # 34;, "34593":"ZL & # 34;, "34660":"YQ & # 34, "34892":"XH & # 34;, "34928":"SC & # 34, "34999":"QJ & # 34;, "35048":"PB & # 34, "35059":"SC & # 34, "35098":"ZC & # 34;, "35203":"TQ & # 34, "35265":"JX & # 34;, "35299":"JX & # 34;, "35782":"深圳& # 34;, "35828":"YS & # 34;, "35830":"E & # 34, "35843":"TD & # 34, "35895":"YG & # 34;, "35977":"MH & # 34, "36158":"JG & # 34;, "36228":"QJ & # 34;, "36426":"XQ & # 34;, "36466":"DC & # 34;, "36710":"JC & # 34, "36711":"ZYG & # 34, "36767":"PB & # 34, "36866":"SK & # 34, "36951":"YW & # 34;, "37034":"YX & # 34, "37063":"XH & # 34;, "37218":"ZC & # 34;, "37325":"ZC & # 34;, "38063":"PB & # 34, "38079":"TD & # 34, "38085":"QY & # 34;, "38107":"DC & # 34;, "38116":"TD & # 34, "38123":"YD & # 34, "38224":"HG & # 34, "38241":"XTC & # 34;, "38271":"ZC & # 34;, "38415":"叶& # 34;, "38426":"KH & # 34, "38461":"YD & # 34, "38463":"AE & # 34, "38466":"PB & # 34, "38477":"XJ & # 34;, "38518":"YT & # 34, "38551":"WK & # 34, "38585":"ZC & # 34;, "38704":"XS & # 34;, "38739":"LJ & # 34, "38761":"GJ & # 34;, "38808":"SQ & # 34, "39048":"JG & # 34;, "39049":"XJ & # 34;, "39052":"HG & # 34, "39076":"CZ & # 34, "39271":"XT & # 34, "39534":"TD & # 34, "39552":"TD & # 34, "39584":"PB & # 34, "39647":" & # 34, "39730":"LG & # 34, "39748":"TPB & # 34;, "40109":"ZQ & # 34;, "40479":"ND & # 34, "40516":"HG & # 34, "40536":"HG & # 34, "40583":"QJ & # 34;, "40765":"YQ & # 34, "40784":"QJ & # 34;, "40840":"YK & # 34;, "40863":"QJG & # 34};

//参数,中文字符串

//返回值:拼音首字母字符串数组

函数makePy() {

var str = document . getelementbyid(& # 34;正文& # 34;).价值;

if (typeof (str)!= "字符串& # 34;) {

抛出新错误(-1,& # 34;\ u51fd \ u 6570 makepy \ u 9700 \ u 8981 \ u5b 57 \ u7b 26 \ u4e 32 \ u7c7b \ u578b \ u53c 2 \ u 6570!");

}

var arr result = new Array();//保存中间结果的数组。

for (var i = 0,len = str.length我& ltleni++) {

//获取unicode代码

var ch = str . charat(I);

//检查unicode代码是否在处理范围内。如果是,则返回该代码对应的汉字的拼音首字母。如果不是,则调用其他函数进行处理。

arr result . push(check ch(ch));

}

//处理arrResult,返回所有可能的拼音初始字符串数组。

document . getelementbyid(& # 34;text _ value & # 34).innerHTML = mkRslt(arrResult)

返回mkr SLT(arr result);

}

函数checkCh(ch) {

var uni = ch . charcode at(0);

//如果不在汉字处理范围内,可以返回原字符,也可以调用自己的处理函数。

if(uni & gt;40869 | | uni & lt19968) {

返回ch;

}//dealwithhothers(ch);

//检查是否是多音字,按多音字处理。否则,直接在strChineseFirstPY字符串中查找对应的首字母。

return (oMultiDiff[uni]?oMultiDiff[uni]:(strchinesefirstpy . charat(uni-19968));

}

函数mkRslt(arr) {

var arrRslt =[& # 34;"];

for (var i = 0,len = arr.length我& ltleni++) {

var str = arr[I];

var strlen = str.length

if (strlen == 1) {

for(var k = 0;k & ltarrRslt.lengthk++) {

arr rslt[k]+= str;

console.log(字符串)

}

}否则{

var TM parr = arrrslt . slice(0);

arr rslt =[];

for(k = 0;k & ltstrlenk++) {

//复制一个相同的arrRslt。

var tmp = TM parr . slice(0);

//将当前字符串[k]添加到每个元素的末尾。

for(var j = 0;j & lttmp.lengthj++) {

tmp[j]+= str . charat(k);

}

//将复制修改后的数组连接到arrRslt。

arr rslt = arr rslt . concat(tmp);

}

}

}

return arrRslt

}

////去掉两端的格函数空

//string . prototype . trim = function(){

//返回this.replace(/(^\s*)|(\s*$)/g,& # 34;");

// };

& lt/script & gt;

& lt/body & gt;

& lt/html & gt;

原文链接:https://blog.csdn.net/ZXW_Future/article/details/89048811

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。

发表回复

登录后才能评论