头像

Thinkphp的提取一个或多个字符串首字母的方法

2017-09-30 08:31:23 来源:PHP代码   浏览()   评论 ( 0 )   

在做项目的时候我们有时会需要提取字符串的首字母

先来看第一种只提取第一个字符的首字母

我们只要在tp的公众函数库中写这样一个方法

function Getzimu($str) 
{ 
$str= iconv("UTF-8","gb2312", $str);//如果程序是gbk的,此行就要注释掉 
if (preg_match("/^[\x7f-\xff]/", $str)) //判断是否全是中文
{ 
$fchar=ord($str{0}); 
if($fchar>=ord("A") and $fchar<=ord("z") )return strtoupper($str{0}); 
$a = $str; 
$val=ord($a{0})*256+ord($a{1})-65536; 
if($val>=-20319 and $val<=-20284)return "A"; 
if($val>=-20283 and $val<=-19776)return "B"; 
if($val>=-19775 and $val<=-19219)return "C"; 
if($val>=-19218 and $val<=-18711)return "D"; 
if($val>=-18710 and $val<=-18527)return "E"; 
if($val>=-18526 and $val<=-18240)return "F"; 
if($val>=-18239 and $val<=-17923)return "G"; 
if($val>=-17922 and $val<=-17418)return "H"; 
if($val>=-17417 and $val<=-16475)return "J"; 
if($val>=-16474 and $val<=-16213)return "K"; 
if($val>=-16212 and $val<=-15641)return "L"; 
if($val>=-15640 and $val<=-15166)return "M"; 
if($val>=-15165 and $val<=-14923)return "N"; 
if($val>=-14922 and $val<=-14915)return "O"; 
if($val>=-14914 and $val<=-14631)return "P"; 
if($val>=-14630 and $val<=-14150)return "Q"; 
if($val>=-14149 and $val<=-14091)return "R"; 
if($val>=-14090 and $val<=-13319)return "S"; 
if($val>=-13318 and $val<=-12839)return "T"; 
if($val>=-12838 and $val<=-12557)return "W"; 
if($val>=-12556 and $val<=-11848)return "X"; 
if($val>=-11847 and $val<=-11056)return "Y"; 
if($val>=-11055 and $val<=-10247)return "Z"; 
} 
else 
{ 
return false; 
} 
}

就可以了

只要在控制器中

$is = '你大爷的';

echo Getzimu($is);die;

就会输出一个N

接下来我们看输出多个字符的首字母

同样的我们只要在公共函数库中写两个方法就行


/** 

* 中文转换到拼音(每次处理一个字符) 

* @param number $iWORD 待处理字符双字节 

* @return string 拼音 

*/ 

function zh2py($iWORD) {
//拼音字符转换图 
$_aMaps = array( 
'A'=>-20319,'AI'=>-20317,'AN'=>-20304,'ANG'=>-20295,'AO'=>-20292, 
'BA'=>-20283,'BAI'=>-20265,'BAN'=>-20257,'BANG'=>-20242,'BAO'=>-20230,'BEI'=>-20051,'BEN'=>-20036,'BENG'=>-20032,'BI'=>-20026,'BIAN'=>-20002,'BIAO'=>-19990,'BIE'=>-19986,'BIN'=>-19982,'BING'=>-19976,'BO'=>-19805,'BU'=>-19784, 
'CA'=>-19775,'CAI'=>-19774,'CAN'=>-19763,'CANG'=>-19756,'CAO'=>-19751,'CE'=>-19746,'CENG'=>-19741,'CHA'=>-19739,'CHAI'=>-19728,'CHAN'=>-19725,'CHANG'=>-19715,'CHAO'=>-19540,'CHE'=>-19531,'CHEN'=>-19525,'CHENG'=>-19515,'CHI'=>-19500,'CHONG'=>-19484,'CHOU'=>-19479,'CHU'=>-19467,'CHUAI'=>-19289,'CHUAN'=>-19288,'CHUANG'=>-19281,'CHUI'=>-19275,'CHUN'=>-19270,'CHUO'=>-19263,'CI'=>-19261,'CONG'=>-19249,'COU'=>-19243,'CU'=>-19242,'CUAN'=>-19238,'CUI'=>-19235,'CUN'=>-19227,'CUO'=>-19224, 
'DA'=>-19218,'DAI'=>-19212,'DAN'=>-19038,'DANG'=>-19023,'DAO'=>-19018,'DE'=>-19006,'DENG'=>-19003,'DI'=>-18996,'DIAN'=>-18977,'DIAO'=>-18961,'DIE'=>-18952,'DING'=>-18783,'DIU'=>-18774,'DONG'=>-18773,'DOU'=>-18763,'DU'=>-18756,'DUAN'=>-18741,'DUI'=>-18735,'DUN'=>-18731,'DUO'=>-18722, 
'E'=>-18710,'EN'=>-18697,'ER'=>-18696, 
'FA'=>-18526,'FAN'=>-18518,'FANG'=>-18501,'FEI'=>-18490,'FEN'=>-18478,'FENG'=>-18463,'FO'=>-18448,'GOU'=>-18447,'GU'=>-18446, 
'GA'=>-18239,'GAI'=>-18237,'GAN'=>-18231,'GANG'=>-18220,'GAO'=>-18211,'GE'=>-18201,'GEI'=>-18184,'GEN'=>-18183,'GENG'=>-18181,'GONG'=>-18012,'GOU'=>-17997,'GU'=>-17988,'GUA'=>-17970,'GUAI'=>-17964,'GUAN'=>-17961,'GUANG'=>-17950,'GUI'=>-17947,'GUN'=>-17931,'GUO'=>-17928, 
'HA'=>-17922,'HAI'=>-17759,'HAN'=>-17752,'HANG'=>-17733,'HAO'=>-17730,'HE'=>-17721,'HEI'=>-17703,'HEN'=>-17701,'HENG'=>-17697,'HONG'=>-17692,'HOU'=>-17683,'HU'=>-17676,'HUA'=>-17496,'HUAI'=>-17487,'HUAN'=>-17482,'HUANG'=>-17468,'HUI'=>-17454,'HUN'=>-17433,'HUO'=>-17427, 
'JI'=>-17417,'JIA'=>-17202,'JIAN'=>-17185,'JIANG'=>-16983,'JIAO'=>-16970,'JIE'=>-16942,'JIN'=>-16915,'JING'=>-16733,'JIONG'=>-16708,'JIU'=>-16706,'JU'=>-16689,'JUAN'=>-16664,'JUE'=>-16657,'JUN'=>-16647, 
'KA'=>-16474,'KAI'=>-16470,'KAN'=>-16465,'KANG'=>-16459,'KAO'=>-16452,'KE'=>-16448,'KEN'=>-16433,'KENG'=>-16429,'KONG'=>-16427,'KOU'=>-16423,'KU'=>-16419,'KUA'=>-16412,'KUAI'=>-16407,'KUAN'=>-16403,'KUANG'=>-16401,'KUI'=>-16393,'KUN'=>-16220,'KUO'=>-16216, 
'LA'=>-16212,'LAI'=>-16205,'LAN'=>-16202,'LANG'=>-16187,'LAO'=>-16180,'LE'=>-16171,'LEI'=>-16169,'LENG'=>-16158,'LI'=>-16155,'LIA'=>-15959,'LIAN'=>-15958,'LIANG'=>-15944,'LIAO'=>-15933,'LIE'=>-15920,'LIN'=>-15915,'LING'=>-15903,'LIU'=>-15889,'LONG'=>-15878,'LOU'=>-15707,'LU'=>-15701,'LV'=>-15681,'LUAN'=>-15667,'LUE'=>-15661,'LUN'=>-15659,'LUO'=>-15652, 
'MA'=>-15640,'MAI'=>-15631,'MAN'=>-15625,'MANG'=>-15454,'MAO'=>-15448,'ME'=>-15436,'MEI'=>-15435,'MEN'=>-15419,'MENG'=>-15416,'MI'=>-15408,'MIAN'=>-15394,'MIAO'=>-15385,'MIE'=>-15377,'MIN'=>-15375,'MING'=>-15369,'MIU'=>-15363,'MO'=>-15362,'MOU'=>-15183,'MU'=>-15180, 
'NA'=>-15165,'NAI'=>-15158,'NAN'=>-15153,'NANG'=>-15150,'NAO'=>-15149,'NE'=>-15144,'NEI'=>-15143,'NEN'=>-15141,'NENG'=>-15140,'NI'=>-15139,'NIAN'=>-15128,'NIANG'=>-15121,'NIAO'=>-15119,'NIE'=>-15117,'NIN'=>-15110,'NING'=>-15109,'NIU'=>-14941,'NONG'=>-14937,'NU'=>-14933,'NV'=>-14930,'NUAN'=>-14929,'NUE'=>-14928,'NUO'=>-14926, 
'O'=>-14922,'OU'=>-14921, 
'PA'=>-14914,'PAI'=>-14908,'PAN'=>-14902,'PANG'=>-14894,'PEO'=>-14889,'PEI'=>-14882,'PEN'=>-14873,'PENG'=>-14871,'PI'=>-14857,'PIAN'=>-14678,'PIAO'=>-14674,'PIE'=>-14670,'PIN'=>-14668,'PING'=>-14663,'PO'=>-14654,'PU'=>-14645, 
'QI'=>-14630,'QIA'=>-14594,'QIAN'=>-14429,'QIANG'=>-14407,'QIAO'=>-14399,'QIE'=>-14384,'QIN'=>-14379,'QING'=>-14368,'QIONG'=>-14355,'QIU'=>-14353,'QU'=>-14345,'QUAN'=>-14170,'QUE'=>-14159,'QUN'=>-14151, 
'RAN'=>-14149,'RANG'=>-14145,'RAO'=>-14140,'RE'=>-14137,'REN'=>-14135,'RENG'=>-14125,'RI'=>-14123,'RONG'=>-14122,'ROU'=>-14112,'RU'=>-14109,'RUAN'=>-14099,'RUI'=>-14097,'RUN'=>-14094,'RUO'=>-14092, 
'SA'=>-14090,'SAI'=>-14087,'SAN'=>-14083,'SANG'=>-13917,'SAO'=>-13914,'SE'=>-13910,'SEN'=>-13907,'SENG'=>-13906,'SHA'=>-13905,'SHAI'=>-13896,'SHAN'=>-13894,'SHANG'=>-13878,'SHAO'=>-13870,'SHE'=>-13859,'SHEN'=>-13847,'SHENG'=>-13831,'SHI'=>-13658,'SHOU'=>-13611,'SHU'=>-13601,'SHUA'=>-13406,'SHUAI'=>-13404,'SHUAN'=>-13400,'SHUANG'=>-13398,'SHUI'=>-13395,'SHUN'=>-13391,'SHUO'=>-13387,'SI'=>-13383,'SONG'=>-13367,'SOU'=>-13359,'SU'=>-13356,'SUAN'=>-13343,'SUI'=>-13340,'SUN'=>-13329,'SUO'=>-13326, 
'TA'=>-13318,'TAI'=>-13147,'TAN'=>-13138,'TANG'=>-13120,'TAO'=>-13107,'TE'=>-13096,'TENG'=>-13095,'TI'=>-13091,'TIAN'=>-13076,'TIAO'=>-13068,'TIE'=>-13063,'TING'=>-13060,'TONG'=>-12888,'TOU'=>-12875,'TU'=>-12871,'TUAN'=>-12860,'TUI'=>-12858,'TUN'=>-12852,'TUO'=>-12849, 
'WA'=>-12838,'WAI'=>-12831,'WAN'=>-12829,'WANG'=>-12812,'WEI'=>-12802,'WEN'=>-12607,'WENG'=>-12597,'WO'=>-12594,'WU'=>-12585, 
'XI'=>-12556,'XIA'=>-12359,'XIAN'=>-12346,'XIANG'=>-12320,'XIAO'=>-12300,'XIE'=>-12120,'XIN'=>-12099,'XING'=>-12089,'XIONG'=>-12074,'XIU'=>-12067,'XU'=>-12058,'XUAN'=>-12039,'XUE'=>-11867,'XUN'=>-11861, 
'YA'=>-11847,'YAN'=>-11831,'YANG'=>-11798,'YAO'=>-11781,'YE'=>-11604,'YI'=>-11589,'YIN'=>-11536,'YING'=>-11358,'YO'=>-11340,'YONG'=>-11339,'YOU'=>-11324,'YU'=>-11303,'YUAN'=>-11097,'YUE'=>-11077,'YUN'=>-11067, 
'ZA'=>-11055,'ZAI'=>-11052,'ZAN'=>-11045,'ZANG'=>-11041,'ZAO'=>-11038,'ZE'=>-11024,'ZEI'=>-11020,'ZEN'=>-11019,'ZENG'=>-11018,'ZHA'=>-11014,'ZHAI'=>-10838,'ZHAN'=>-10832,'ZHANG'=>-10815,'ZHAO'=>-10800,'ZHE'=>-10790,'ZHEN'=>-10780,'ZHENG'=>-10764,'ZHI'=>-10587,'ZHONG'=>-10544,'ZHOU'=>-10533,'ZHU'=>-10519,'ZHUA'=>-10331,'ZHUAI'=>-10329,'ZHUAN'=>-10328,'ZHUANG'=>-10322,'ZHUI'=>-10315,'ZHUN'=>-10309,'ZHUO'=>-10307,'ZI'=>-10296,'ZONG'=>-10281,'ZOU'=>-10274,'ZU'=>-10270,'ZUAN'=>-10262,'ZUI'=>-10260,'ZUN'=>-10256,'ZUO'=>-10254 
);
if($iWORD>0 && $iWORD<160 ) { 
return chr($iWORD); 
} elseif ($iWORD<-20319||$iWORD>-10247) { 
return ''; 
} else { 
foreach ($_aMaps as $py => $code) { 
if($code > $iWORD) break; 
$result = $py; 
} 
return $result; 
} 
} 
/** 
* 将中文编码成拼音 
* @param string $utf8Data utf8字符集数据 
* @param string $sRetFormat 返回格式 [head:首字母|all:全拼音] 
* @return string 
*/ 
function encode($utf8Data, $sRetFormat='head'){ 
$sGBK = iconv('UTF-8', 'GBK', $utf8Data); 
$aBuf = array(); 
for ($i=0, $iLoop=strlen($sGBK); $i<$iLoop; $i++) { 
$iChr = ord($sGBK{$i}); 
if ($iChr>160) 
$iChr = ($iChr<<8) + ord($sGBK{++$i}) - 65536; 
if ('head' === $sRetFormat) 
$aBuf[] = substr(zh2py($iChr),0,1); 
else 
$aBuf[] = zh2py($iChr); 
} 
if ('head' === $sRetFormat) 
return implode('', $aBuf); 
else 
return implode(' ', $aBuf); 
} 
在控制其中输入
$is = '你大爷的';
echo encode($is);die;
就会输出NDYD

是不是很简单,点个赞收藏啊一下下楼;


标签: phpmysql
声明:转载请注明来源(PHP代码)并保留原文链接:https://www.phpdaima.com//wenzhang-14.html
广告不存在
评论0

后面还有条评论,点击查看>>

温馨提示:为规范评论内容,垃圾评论一律封号...