获取汉字首字母(php,java)

获取单个汉字的拼音首字母

php源码:

<?php
//判断ascii值是否在该区间
function Between($val, $lower, $upper){
    return ($val >= $lower) && ($val <= $upper);
}
//取字符
function GetPinyinHead($word){
	//取高8位
	$high = ord($word{0});
	//取低八位
	$low = ord($word{1});
	//转整数值
    $hexc = $high * 256 + $low;
	//比较值的区间
    if (Between($hexc, 0xB0A1, 0xB0C4)) return 'A';
    if (Between($hexc, 0XB0C5, 0XB2C0)) return 'B';
    if (Between($hexc, 0xB2C1, 0xB4ED)) return 'C';
    if (Between($hexc, 0xB4EE, 0xB6E9)) return 'D';
    if (Between($hexc, 0xB6EA, 0xB7A1)) return 'E';
    if (Between($hexc, 0xB7A2, 0xB8c0)) return 'F';
    if (Between($hexc, 0xB8C1, 0xB9FD)) return 'G';
    if (Between($hexc, 0xB9FE, 0xBBF6)) return 'H';
    if (Between($hexc, 0xBBF7, 0xBFA5)) return 'J';
    if (Between($hexc, 0xBFA6, 0xC0AB)) return 'K';
    if (Between($hexc, 0xC0AC, 0xC2E7)) return 'L';
    if (Between($hexc, 0xC2E8, 0xC4C2)) return 'M';
    if (Between($hexc, 0xC4C3, 0xC5B5)) return 'N';
    if (Between($hexc, 0xC5B6, 0xC5BD)) return 'O';
    if (Between($hexc, 0xC5BE, 0xC6D9)) return 'P';
    if (Between($hexc, 0xC6DA, 0xC8BA)) return 'Q';
    if (Between($hexc, 0xC8BB, 0xC8F5)) return 'R';
    if (Between($hexc, 0xC8F6, 0xCBF0)) return 'S';   
    if (Between($hexc, 0xCBFA, 0xCDD9)) return 'T';
    if (Between($hexc, 0xCDDA, 0xCEF3)) return 'W';
    if (Between($hexc, 0xCEF4, 0xD188)) return 'X';
    if (Between($hexc, 0xD1B9, 0xD4D0)) return 'Y';
    if (Between($hexc, 0xD4D1, 0xD7F9)) return 'Z';
	//找不到的值返回#号
    return '#';
}

/*
*@author com.love
*单个中文字获取拼音的首字母
*太难的字无法识别 例如 熏
*@date 2015 12 08
*/
$word = iconv("utf-8","gb2312",'宏');
//一定是GB2312字符
echo GetPinyinHead($word);
?>

java源码:

package com.love.test;

import java.io.UnsupportedEncodingException;

public class Pingyin {
	//获取首字母
	public static void main(String[] args) throws UnsupportedEncodingException {
		Pingyin pinyin = new Pingyin();
		char s=pinyin.GetPinyinHead("维");
		System.out.println(s);
	}
	
	//比较2者之间的ascii值
	public boolean Between(int value,int lower,int upper){
		return (value>=lower)&&(value<=upper);
	}
	
	//获取单个汉字的 首字母
	public char GetPinyinHead(String word) throws UnsupportedEncodingException{
		byte [] bs = word.getBytes("GB2312");
		//to hex
		int hexc = (bs[1] & 0xff) | ((bs[0] << 8) & 0xff00);
		//比较
	    if (Between(hexc, 0xB0A1, 0xB0C4)) return 'A';
	    if (Between(hexc, 0XB0C5, 0XB2C0)) return 'B';
	    if (Between(hexc, 0xB2C1, 0xB4ED)) return 'C';
	    if (Between(hexc, 0xB4EE, 0xB6E9)) return 'D';
	    if (Between(hexc, 0xB6EA, 0xB7A1)) return 'E';
	    if (Between(hexc, 0xB7A2, 0xB8c0)) return 'F';
	    if (Between(hexc, 0xB8C1, 0xB9FD)) return 'G';
	    if (Between(hexc, 0xB9FE, 0xBBF6)) return 'H';
	    if (Between(hexc, 0xBBF7, 0xBFA5)) return 'J';
	    if (Between(hexc, 0xBFA6, 0xC0AB)) return 'K';
	    if (Between(hexc, 0xC0AC, 0xC2E7)) return 'L';
	    if (Between(hexc, 0xC2E8, 0xC4C2)) return 'M';
	    if (Between(hexc, 0xC4C3, 0xC5B5)) return 'N';
	    if (Between(hexc, 0xC5B6, 0xC5BD)) return 'O';
	    if (Between(hexc, 0xC5BE, 0xC6D9)) return 'P';
	    if (Between(hexc, 0xC6DA, 0xC8BA)) return 'Q';
	    if (Between(hexc, 0xC8BB, 0xC8F5)) return 'R';
	    if (Between(hexc, 0xC8F6, 0xCBF0)) return 'S';   
	    if (Between(hexc, 0xCBFA, 0xCDD9)) return 'T';
	    if (Between(hexc, 0xCDDA, 0xCEF3)) return 'W';
	    if (Between(hexc, 0xCEF4, 0xD188)) return 'X';
	    if (Between(hexc, 0xD1B9, 0xD4D0)) return 'Y';
	    if (Between(hexc, 0xD4D1, 0xD7F9)) return 'Z';
		return '#';
	}
}

原创: http://blog.csdn.net/qilin001cs



你可能感兴趣的:(java,PHP,汉字首字母)