很多站长朋友们都不太清楚php获取首写字母,今天小编就来给大家整理php获取首写字母,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 [转载]PHP如何取得汉字的拼音首字母 2、 求php获取首字母方法 3、 php获取字符串首字母 4、 php批量获取首字母(汉字、数字、英文) 5、 [php教程]如何用PHP实现取字符的首字母? 6、 PHP自定义函数获取汉字首字母的方法 [转载]PHP如何取得汉字的拼音首字母函数返
单
汉字
拼音首字母
需要处理
文字符串
需要重新写
函数
用
取
串汉字
拼音首字母
$s1);if($s2==$zh){$zh=$s1;}for($i=0;$i<strlen($zh);$i++){$s1=substr($zh,$i,1);$p=ord($s1);if($p160){$s2=substr($zh,$i++,2);$ret.=
求php获取首字母方法function ($str){
$s1 = iconv('UTF-8','gb2312',$str);
$s2 = iconv('gb2312','UTF-8',$s1);
$s = $s2 == $str ? $s1 : $str;
$asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
if($asc >= -20319 $asc <= -20284) return 'a';
if($asc >= -20283 $asc <= -19776) return 'b';
if($asc >= -19775 $asc <= -19219) return 'c';
if($asc >= -19218 $asc <= -18711) return 'd';
if($asc >= -18710 $asc <= -18527) return 'e';
if($asc >= -18526 $asc <= -18240) return 'f';
if($asc >= -18239 $asc <= -17923) return 'g';
if($asc >= -17922 $asc <= -17418) return 'h';
if($asc >= -17417 $asc <= -16475) return 'j';
if($asc >= -16474 $asc <= -16213) return 'k';
if($asc >= -16212 $asc <= -15641) return 'l';
if($asc >= -15640 $asc <= -15166) return 'm';
if($asc >= -15165 $asc <= -14923) return 'n';
if($asc >= -14922 $asc <= -14915) return 'o';
if($asc >= -14914 $asc <= -14631) return 'p';
if($asc >= -14630 $asc <= -14150) return 'q';
if($asc >= -14149 $asc <= -14091) return 'r';
if($asc >= -14090 $asc <= -13319) return 's';
if($asc >= -13318 $asc <= -12839) return 't';
if($asc >= -12838 $asc <= -12557) return 'w';
if($asc >= -12556 $asc <= -11848) return 'x';
if($asc >= -11847 $asc <= -11056) return 'y';
if($asc >= -11055 $asc <= -10247) return 'z';
return null;
};
这是一个传入汉字,通过汉字的ASCII码来判断汉字的首字母的方法。已经调试,可以直接使用。望测试。
php获取字符串首字母他是根据汉字gb2312编码的头两个字符的范围来区分的。
识别不出很正常,因为gb2312本身包含的汉字就不全。你搜个utf8汉字转拼音的,你再修改下取个首字符。应该好点
php批量获取首字母(汉字、数字、英文)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
[php教程]如何用PHP实现取字符的首字母?<?php
function getfirstchar($s0){
$c=ereg('[a-zA-Z]', strtoupper(substr( $s0, 0, 1 )));
if($c){
return strtoupper(substr( $s0, 0, 1 )) ;
}else{
if($fchar>=ord("a") and $fchar<=ord("Z") )return strtoupper($s0{0});
if(is_numeric(substr( $s0, 0, 1 ))){
$s0 =ToChinaseNum (substr( $s0, 0, 1 ));
}
$s= $s0;
$asc=ord($s{0})*256+ord($s{1})-65536;
if($asc>=-20319 and $asc<=-20284)return "A";
if($asc>=-20283 and $asc<=-19776)return "B";
if($asc>=-19775 and $asc<=-19219)return "C";
if($asc>=-19218 and $asc<=-18711)return "D";
if($asc>=-18710 and $asc<=-18527)return "E";
if($asc>=-18526 and $asc<=-18240)return "F";
if($asc>=-18239 and $asc<=-17923)return "G";
if($asc>=-17922 and $asc<=-17418)return "H";
if($asc>=-17417 and $asc<=-16475)return "J";
if($asc>=-16474 and $asc<=-16213)return "K";
if($asc>=-16212 and $asc<=-15641)return "L";
if($asc>=-15640 and $asc<=-15166)return "M";
if($asc>=-15165 and $asc<=-14923)return "N";
if($asc>=-14922 and $asc<=-14915)return "O";
if($asc>=-14914 and $asc<=-14631)return "P";
if($asc>=-14630 and $asc<=-14150)return "Q";
if($asc>=-14149 and $asc<=-14091)return "R";
if($asc>=-14090 and $asc<=-13319)return "S";
if($asc>=-13318 and $asc<=-12839)return "T";
if($asc>=-12838 and $asc<=-12557)return "W";
if($asc>=-12556 and $asc<=-11848)return "X";
if($asc>=-11847 and $asc<=-11056)return "Y";
if($asc>=-11055 and $asc<=-10247)return "Z";
return null;
}
}
function ToChinaseNum($num)
{
$char = array("零","一","二","三","四","五","六","七","八","九");
$dw = array("","十","百","千","万","亿","兆");
$retval = "";
$proZero = false;
for($i = 0;$i < strlen($num);$i++)
{
if($i > 0) $temp = (int)(($num % pow (10,$i+1)) / pow (10,$i));
else $temp = (int)($num % pow (10,1));
if($proZero == true $temp == 0) continue;
if($temp == 0) $proZero = true;
else $proZero = false;
if($proZero)
{
if($retval == "") continue;
$retval = $char[$temp].$retval;
}
else $retval = $char[$temp].$dw[$i].$retval;
}
if($retval == "一十") $retval = "十";
return $retval;
}
var_dump(getfirstchar("89ssss"));
?>
PHP自定义函数获取汉字首字母的方法本文实例讲述了PHP自定义函数获取汉字首字母的方法。分享给大家供大家参考,具体如下:
首字母很重要,可以进行排序使用。
城市列表等等。
<?php
/*
*
Created
on
2016-12-1
*/
function
getFirstCharter($str)
{
if
(empty($str))
{
return
'';
}
$fchar
=
ord($str{0});
if
($fchar
>=
ord('A')
$fchar
<=
ord('z'))
return
strtoupper($str{0});
$s1
=
iconv('UTF-8',
'gb2312',
$str);
$s2
=
iconv('gb2312',
'UTF-8',
$s1);
$s
=
$s2
==
$str
?
$s1
:
$str;
$asc
=
ord($s{0})
*
256
+
ord($s{1})
-
65536;
if
($asc
>=
-20319
$asc
<=
-20284)
return
'A';
if
($asc
>=
-20283
$asc
<=
-19776)
return
'B';
if
($asc
>=
-19775
$asc
<=
-19219)
return
'C';
if
($asc
>=
-19218
$asc
<=
-18711)
return
'D';
if
($asc
>=
-18710
$asc
<=
-18527)
return
'E';
if
($asc
>=
-18526
$asc
<=
-18240)
return
'F';
if
($asc
>=
-18239
$asc
<=
-17923)
return
'G';
if
($asc
>=
-17922
$asc
<=
-17418)
return
'H';
if
($asc
>=
-17417
$asc
<=
-16475)
return
'J';
if
($asc
>=
-16474
$asc
<=
-16213)
return
'K';
if
($asc
>=
-16212
$asc
<=
-15641)
return
'L';
if
($asc
>=
-15640
$asc
<=
-15166)
return
'M';
if
($asc
>=
-15165
$asc
<=
-14923)
return
'N';
if
($asc
>=
-14922
$asc
<=
-14915)
return
'O';
if
($asc
>=
-14914
$asc
<=
-14631)
return
'P';
if
($asc
>=
-14630
$asc
<=
-14150)
return
'Q';
if
($asc
>=
-14149
$asc
<=
-14091)
return
'R';
if
($asc
>=
-14090
$asc
<=
-13319)
return
'S';
if
($asc
>=
-13318
$asc
<=
-12839)
return
'T';
if
($asc
>=
-12838
$asc
<=
-12557)
return
'W';
if
($asc
>=
-12556
$asc
<=
-11848)
return
'X';
if
($asc
>=
-11847
$asc
<=
-11056)
return
'Y';
if
($asc
>=
-11055
$asc
<=
-10247)
return
'Z';
return
null;
}
$firstChar
=
getFirstCharter('脚本之家');
print_r($firstChar);//输出:J
?>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php常用函数与技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP错误与异常处理方法总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
关于php获取首写字母的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php获取首写字母 php首字母大写函数的详细内容...