php批量获取首字母(汉字、数字、英文)

php批量获取首字母(汉字、数字、英文),第1张

php批量获取首字母(汉字 数字 英文)

$mysql_server_name= ; //改成自己的mysql数据库服务器

$mysql_username= 用户 ; //改成自己的mysql数据库用户名

$mysql_password= 密码 ; //改成自己的mysql数据库密码

$mysql_database= 数据库 ; //改成自己的mysql数据库名

mysql_connect( $mysql_username $mysql_password) or die( database not access );

mysql_select_db($mysql_database);

mysql_query("SET NAMES utf ");

$equery = " select title from 表 ";

$result =mysql_query($equery );

while ($row = mysql_fetch_array($result MYSQL_BOTH))

{

$title=$row["title"];

if (ord($title)> ) { //汉字开头

echo $letter=getfirstchar($title);

}else if(ord($title)>= and ord($title)<= ){ //数字开头

echo $letter=iconv_substr($title utf );

}else if(ord($title)>= and ord($title)<= ){ //大写英文开头

echo $letter=iconv_substr($title utf );

}else if(ord($title)>= and ord($title)<= ){ //小写英文开头

echo $letter=iconv_substr($title utf );

}

}

function getfirstchar($s ){

$s=iconv("UTF " "gb " $s );

$asc=ord($s{ }) +ord($s{ }) ;

if($asc>= and $asc<= )return "A";

if($asc>= and $asc<= )return "B"; if($asc>= and $asc<= )return "C";

if($asc>= and $asc<= )return "D";

if($asc>= and $asc<= )return "E";

if($asc>= and $asc<= )return "F";

if($asc>= and $asc<= )return "G";

if($asc>= and $asc<= )return "H";

if($asc>= and $asc<= )return "J";

if($asc>= and $asc<= )return "K";

if($asc>= and $asc<= )return "L";

if($asc>= and $asc<= )return "M";

if($asc>= and $asc<= )return "N";

if($asc>= and $asc<= )return "O";

if($asc>= and $asc<= )return "P";

if($asc>= and $asc<= )return "Q";

if($asc>= and $asc<= )return "R";

if($asc>= and $asc<= )return "S";

if($asc>= and $asc<= )return "T";

if($asc>= and $asc<= )return "W";

if($asc>= and $asc<= )return "X";

if($asc>= and $asc<= )return "Y";

if($asc>= and $asc<= )return "Z";

return false;

lishixinzhi/Article/program/PHP/201311/20913

步骤一:将下述常量数组定义为名称,如PY,下面就是包括26个字母及其第一个汉字: ={"","";"吖","A";"八","B";"嚓","C";"咑","D";"鵽","E";"发","F";"猤","G";"铪","H";" 夻","J";"咔","K";"垃","L";"呒","M";"旀","N";"噢","O";"妑","P";"七","Q";"囕","R";" 仨","S";"他","T";"屲","W";"夕","X";"丫","Y";"帀","Z"}

复制公式或代码步骤二:使用LOOKUP(汉字,PY)或者VLOOKUP(汉字,PY,2)就可以返回指定汉字的拼音首字母:

(注意:使用VLOOKUP()函数时,其最后一个参数须用1或True的模糊“默认”查找方式)

如下,LOOKUP()的个数可根据汉字的个数决定,由于上面定义的PY最前面有一个""的值,所以可以屏蔽错误,当然也可以用迭代计算的方式获得: =LOOKUP(LEFT(A2),PY)&LOOKUP(

获取首字母需要对汉字表和字母表进行映射,如下示例代码是以gb2312编码为入手点,进行匹配的,也可以使用gbk、utf-8等编码进行匹配,但代码就完全不同了。

示例代码如下:

public class FirstLetterUtils {

// 简体中文的编码范围从B0A1(45217)一直到F7FE(63486)

private static int BEGIN = 45217;

private static int END = 63486;

// 按照声 母表示,这个表是在GB2312中的出现的第一个汉字,也就是说“啊”是代表首字母a的第一个汉字。

// i, u, v都不做声母, 自定规则跟随前面的字母

private static char[] chartable = { '啊', '芭', '擦', '搭', '蛾', '发', '噶', '哈', '哈', '击', '喀', '垃', '妈', '拿', '哦', '啪', '期', '然', '撒', '塌', '塌', '塌', '挖', '昔', '压', '匝', };

// 二十六个字母区间对应二十七个端点

// GB2312码汉字区间十进制表示

private static int[] table = new int[27];

// 对应首字母区间表

private static char[] initialtable = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 't', 't', 'w', 'x', 'y', 'z', };

// 初始化

static {

for (int i = 0; i < 26; i++) {

table[i] = gbValue(chartable[i]);// 得到GB2312码的首字母区间端点表,十进制。

}

table[26] = END;// 区间表结尾

}

// ------------------------public方法区------------------------

// 根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串 最重要的一个方法,思路如下:一个个字符读入、判断、输出

public static String cn2py(String SourceStr) {

String Result = "";

int StrLength = SourceStrlength();

int i;

try {

for (i = 0; i < StrLength; i++) {

Result += Char2Initial(SourceStrcharAt(i));

}

} catch (Exception e) {

Result = "";

eprintStackTrace();

}

return Result;

}

// ------------------------private方法区------------------------

/

输入字符,得到他的声母,英文字母返回对应的大写字母,其他非简体汉字返回 '0'

/

private static char Char2Initial(char ch) {

// 对英文字母的处理:小写字母转换为大写,大写的直接返回

if (ch >= 'a' && ch <= 'z') {

return (char) (ch - 'a' + 'A');

}

if (ch >= 'A' && ch <= 'Z') {

return ch;

}

// 对非英文字母的处理:转化为首字母,然后判断是否在码表范围内,

// 若不是,则直接返回。

// 若是,则在码表内的进行判断。

int gb = gbValue(ch);// 汉字转换首字母

if ((gb < BEGIN) || (gb > END))// 在码表区间之前,直接返回

{

return ch;

}

int i;

for (i = 0; i < 26; i++) {// 判断匹配码表区间,匹配到就break,判断区间形如“[,)”

if ((gb >= table[i]) && (gb < table[i + 1])) {

break;

}

}

if (gb == END) {// 补上GB2312区间最右端

i = 25;

}

return initialtable[i]; // 在码表区间中,返回首字母

}

/

取出汉字的编码 cn 汉字

/

private static int gbValue(char ch) {// 将一个汉字(GB2312)转换为十进制表示。

String str = new String();

str += ch;

try {

byte[] bytes = strgetBytes("GB2312");

if (byteslength < 2) {

return 0;

}

return (bytes[0] << 8 & 0xff00) + (bytes[1] & 0xff);

} catch (Exception e) {

return 0;

}

}

public static void main(String[] args) throws Exception {

Systemoutprintln(cn2py("这是一个获取首字母的class"));

}

}

在EXECL中 ,按ALT+F11,插入---模块 复制下列代码 :

Function hztopy(hzpy As String) As String

Dim hzstring As String, pystring As String

Dim hzpysum As Integer, hzi As Integer, hzpyhex As Integer

hzstring = Trim(hzpy)

hzpysum = Len(Trim(hzstring))

pystring = ""

For hzi = 1 To hzpysum

hzpyhex = "&H" + Hex(Asc(Mid(hzstring, hzi, 1)))

Select Case hzpyhex

Case &HB0A1 To &HB0C4: pystring = pystring + "A"

Case &HB0C5 To &HB2C0: pystring = pystring + "B"

Case &HB2C1 To &HB4ED: pystring = pystring + "C"

Case &HB4EE To &HB6E9: pystring = pystring + "D"

Case &HB6EA To &HB7A1: pystring = pystring + "E"

Case &HB7A2 To &HB8C0: pystring = pystring + "F"

Case &HB8C1 To &HB9FD: pystring = pystring + "G"

Case &HB9FE To &HBBF6: pystring = pystring + "H"

Case &HBBF7 To &HBFA5: pystring = pystring + "J"

Case &HBFA6 To &HC0AB: pystring = pystring + "K"

Case &HC0AC To &HC2E7: pystring = pystring + "L"

Case &HC2E8 To &HC4C2: pystring = pystring + "M"

Case &HC4C3 To &HC5B5: pystring = pystring + "N"

Case &HC5B6 To &HC5BD: pystring = pystring + "O"

Case &HC5BE To &HC6D9: pystring = pystring + "P"

Case &HC6DA To &HC8BA: pystring = pystring + "Q"

Case &HC8BB To &HC8F5: pystring = pystring + "R"

Case &HC8F6 To &HCBF9: pystring = pystring + "S"

Case &HCBFA To &HCDD9: pystring = pystring + "T"

Case &HEDC5: pystring = pystring + "T"

Case &HCDDA To &HCEF3: pystring = pystring + "W"

Case &HCEF4 To &HD1B8: pystring = pystring + "X"

Case &HD1B9 To &HD4D0: pystring = pystring + "Y"

Case &HD4D1 To &HD7F9: pystring = pystring + "Z"

Case Else

pystring = pystring + Mid(hzstring, hzi, 1)

End Select

Next

hztopy = pystring

End Function

比如在A1输入 小燕子耳坠子78 则在其他空白单元格输入 =hztopy(A1) 回车执行即可

以上就是关于php批量获取首字母(汉字、数字、英文)全部的内容,包括:php批量获取首字母(汉字、数字、英文)、excel中如何实现提取汉字的拼音首字母、java怎么根据汉字获取字的拼音首字母等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9351453.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存