javascript汉字获取拼音首字符问题?

JavaScript09

javascript汉字获取拼音首字符问题?,第1张

那个是VBscript的函数,js里面没有获取GB2312编码的函数,需要自己整一个。

GB2312里面的7k多个汉字是按照拼音排序的,后来扩充的汉字并没有按照拼音排序,(为了不打乱已经排好的GB2312编码表),所以要准确获取拼音最好还是用词库来比对。

这里有比较好的例子:

http://www.cssrain.cn/demo/pinyin/lukin/Pinyin.html

自己改写一下就可以。

如果要搞好那个函数,需要把编码转换为ansi编码,网上有很多这里函数。

复杂的汉字确实不能找到正确的拼音,只适用简单的汉字GB2312里的。

改写如下:

先去http://www.cftea.com/c/2009/04/3T7WXDO9S9VFBXH3.asp 里弄两个函数来:字数超过限制就不贴这两个函数了。自己去下载一下:

function UnicodeChr()

function AnsicodeChr()

var s='拼音转换'

alert( getPY_str(s) )

function getPY_str(str){

var py_str=''

for ( var i=0i < str.length i++)

{

py_str +=getPY(str.substr(i,1))

}

return py_str

}

function ansi(c){

return UnicodeToAnsi(c.charCodeAt(0))

}

function UnicodeToAnsi(chrCode)

{

var chrHex=chrCode.toString(16)

chrHex="000"+chrHex.toUpperCase()

chrHex=chrHex.substr(chrHex.length-4)

var i=UnicodeChr().indexOf(chrHex)

if(i!=-1)

{

chrHex=AnsicodeChr().substr(i,4)

}

return parseInt(chrHex,16)

}

function getPY(ch)

{

if(ch !='') {

var code = ansi(ch)

var py = ''

if(code>=45217 &&code<=45252) {

py = "A"

} else if(code>=45253 &&code<=45760) {

py = "B"

} else if(code>=45761 &&code<=46317) {

py = "C"

} else if(code>=46318 &&code<=46825) {

py = "D"

} else if(code>=46826 &&code<=47009) {

py = "E"

} else if(code>=47010 &&code<=47296) {

py = "F"

} else if((code>=47297 &&code<=47613) || (code == 63193)) {

py = "G"

} else if(code>=47614 &&code<=48118) {

py = "H"

} else if(code>=48119 &&code<=49061) {

py = "J"

} else if(code>=49062 &&code<=49323) {

py = "K"

} else if(code>=49324 &&code<=49895) {

py = "L"

} else if(code>=49896 &&code<=50370) {

py = "M"

} else if(code>=50371 &&code<=50613) {

py = "N"

} else if(code>=50614 &&code<=50621) {

py = "O"

} else if(code>=50622 &&code<=50905) {

py = "P"

} else if(code>=50906 &&code<=51386) {

py = "Q"

} else if(code>=51387 &&code<=51445) {

py = "R"

} else if(code>=51446 &&code<=52217) {

py = "S"

} else if(code>=52218 &&code<=52697) {

py = "T"

} else if(code>=52698 &&code<=52979) {

py = "W"

} else if(code>=52980 &&code<=53688) {

py = "X"

} else if(code>=53689 &&code<=54480) {

py = "Y"

} else if(code>=54481 &&code<=62289) {

py = "Z"

} else {

py =ch

}

return py

}else{

return ''

}

}

1、新建一个html文件,命名为test.html。

2、在test.html文件内,在p标签内,使用input标签创建一个字符串输入框。

3、在test.html文件内,设置input元素的id为myinput,主要用于下面通过该id获得input对象。

4、在test.html文件内,使用button标签创建一个按钮。

5、在test.html文件中,给button按钮绑定onclick点击事件,当按钮被点击时,执行getlast()函数。

6、在js标签中,创建getlast()函数,在函数内,通过getElementById()方法获得input对象。

7、在浏览器打开test.html文件,点击按钮,查看结果。

//参数,中文字符串

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

function makePy(str){  

if(typeof(str) != "string")  

throw new Error(-1,"函数makePy需要字符串类型参数!")  

var arrResult = new Array() //保存中间结果的数组  

for(var i=0,len=str.lengthi<leni++){  

//获得unicode码  

var ch = str.charAt(i)  

//检查该unicode码是否在处理范围之内,在则返回该码对映汉字的拼音首字母,不在则调用其它函数处理  

arrResult.push(checkCh(ch))  

}  

//处理arrResult,返回所有可能的拼音首字母串数组  

return mkRslt(arrResult)  

}  

function checkCh(ch){  

var uni = ch.charCodeAt(0)  

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

if(uni > 40869 || uni < 19968)  

return ch //dealWithOthers(ch)  

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

return (oMultiDiff[uni]?oMultiDiff[uni]:(strChineseFirstPY.charAt(uni-19968)))  

}  

function mkRslt(arr){  

var arrRslt = [""]  

for(var i=0,len=arr.lengthi<leni++){  

var str = arr[i]  

var strlen = str.length  

if(strlen == 1){  

for(var k=0k<arrRslt.lengthk++){  

arrRslt[k] += str  

}  

}else{  

var tmpArr = arrRslt.slice(0)  

arrRslt = []  

for(k=0k<strlenk++){  

//复制一个相同的arrRslt  

var tmp = tmpArr.slice(0)  

//把当前字符str[k]添加到每个元素末尾  

for(var j=0j<tmp.lengthj++){  

tmp[j] += str.charAt(k)  

}  

//把复制并修改后的数组连接到arrRslt上  

arrRslt = arrRslt.concat(tmp)  

}  

}  

}  

return arrRslt  

}  

//两端去空格函数  

String.prototype.trim = function() {    return this.replace(/(^\s*)|(\s*$)/g,"") }  

  

//查看拼音首字母缩写  

function query(){  

    var str = document.getElementById("txtChinese").value.trim()  

    if(str == "") return  

    var arrRslt = makePy(str)  

    alert(arrRslt)