本文目录一览:
谁有php 汉字转拼音的 代码
php 汉字转拼音的 代码
?php
/**
* PHP 汉字转拼音
* @author Jerryli(hzjerry@gmail.com)
* @version V0.20140715
* @package SPFW.core.lib.final
* @global SEA_PHP_FW_VAR_ENV
* @example
* echo CUtf8_PY::encode('阿里巴巴科技有限公司'); //编码为拼音首字母
* echo CUtf8_PY::encode('阿里巴巴科技有限公司', 'all'); //编码为全拼音
*/
class CUtf8_PY {
/**
* 拼音字符转换图
* @var array
*/
private static $_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,'fou'=-18447,'fu'=-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'=-
php 中文转拼音首字母问题
原始代码有些问题,我做了些更改,原地址github 的 chenall/chenall/blob/master/php/tools/ZH-cn_TO_pinyin.php
?php
/*
中文汉字转拼音首字母的PHP简易实现方法.
要求: 只能是GB2312码表里面中文字符
转换得到字符串对应的拼音首字母大写.
用法:
echo zh2py::conv('Chinese 中华人民共和国');//Chinese ZHRMGHG
或
$py = new zh2py;
echo $py-conv('Chinese 中华人民共和国');//Chinese ZHRMGHG
*/
class zh2py
{
//根据汉字区位表
//我们可以看到从16-55区之间是按拼音字母排序的,所以我们只需要判断某个汉字的区位码就可以得知它的拼音首字母.
//区位表第一部份,按拼音字母排序的.
//16区-55区
/*
'A'=0xB0A1, 'B'=0xB0C5, 'C'=0xB2C1, 'D'=0xB4EE, 'E'=0xB6EA, 'F'=0xB7A2, 'G'=0xB8C1,'H'=0xB9FE,
'J'=0xBBF7, 'K'=0xBFA6, 'L'=0xC0AC, 'M'=0xC2E8, 'N'=0xC4C3, 'O'=0xC5B6, 'P'=0xC5BE,'Q'=0xC6DA,
'R'=0xC8BB, 'S'=0xC8F6, 'T'=0xCBFA, 'W'=0xCDDA, 'X'=0xCEF4, 'Y'=0xD1B9, 'Z'=0xD4D1
*/
private static $FirstTable = array(
0xB0C5, 0xB2C1, 0xB4EE, 0xB6EA, 0xB7A2, 0xB8C1, 0xB9FE, 0xBBF7, 0xBFA6, 0xC0AC, 0xC2E8,
0xC4C3, 0xC5B6, 0xC5BE, 0xC6DA, 0xC8BB, 0xC8F6, 0xCBFA, 0xCDDA, 0xCEF4, 0xD1B9, 0xD4D1,0xD7FA
);
private static $FirstLetter = "ABCDEFGHJKLMNOPQRSTWXYZ";
//区位表第二部份,不规则的,下面的字母是每个区里面对应字的拼音首字母.从网上查询整理出来的,可能会有部份错误.
//56区-87区
private static $SecondTable = array(
"CJWGNSPGCGNEGYPBTYYZDXYKYGTZJNMJQMBSGZSCYJSYYFPGKBZGYDYWJKGKLJSWKPJQHYJWRDZLSYMRYPYWWCCKZNKYYG",
"TTNGJEYKKZYTCJNMCYLQLYPYSFQRPZSLWBTGKJFYXJWZLTBNCXJJJJTXDTTSQZYCDXXHGCKBPHFFSSTYBGMXLPBYLLBHLX",
"SMZMYJHSOJNGHDZQYKLGJHSGQZHXQGKXZZWYSCSCJXYEYXADZPMDSSMZJZQJYZCJJFWQJBDZBXGZNZCPWHWXHQKMWFBPBY",
"DTJZZKXHYLYGXFPTYJYYZPSZLFCHMQSHGMXXSXJYQDCSBBQBEFSJYHWWGZKPYLQBGLDLCDTNMAYDDKSSNGYCSGXLYZAYPN",
"PTSDKDYLHGYMYLCXPYCJNDQJWXQXFYYFJLEJPZRXCCQWQQSBZKYMGPLBMJRQCFLNYMYQMSQYRBCJTHZTQFRXQHXMQJCJLY",
"QGJMSHZKBSWYEMYLTXFSYDXWLYCJQXSJNQBSCTYHBFTDCYZDJWYGHQFRXWCKQKXEBPTLPXJZSRMEBWHJLBJSLYYSMDXLCL",
"QKXLHXJRZJMFQHXHWYWSBHTRXXGLHQHFNMGYKLDYXZPYLGGSMTCFBAJJZYLJTYANJGBJPLQGSZYQYAXBKYSECJSZNSLYZH",
"ZXLZCGHPXZHZNYTDSBCJKDLZAYFFYDLEBBGQYZKXGLDNDNYSKJSHDLYXBCGHXYPKDJMMZNGMMCLGWZSZXZJFZNMLZZTHCS",
"YDBDLLSCDDNLKJYKJSYCJLKWHQASDKNHCSGAGHDAASHTCPLCPQYBSZMPJLPCJOQLCDHJJYSPRCHNWJNLHLYYQYYWZPTCZG",
"WWMZFFJQQQQYXACLBHKDJXD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YFBPNLSFHTGJWEJJXXGLLJSTGSHJQLZFKCGNNNSZFDEQFHBS",
"AQTGYLBXMMYGSZLDYDQMJJRGBJTKGDHGKBLQKBDMBYLXWCXYTTYBKMRTJZXQJBHLMHMJJZMQASLDCYXYQDLQCAFYWYXQHZ",
);
public static function utf8_to_gbk($string)//编码转换,必须转换成GB2312字符,这里只是简单的判断并不是很准确,可以自己写一个.
{
if (mb_check_encoding($string,'gb2312'))
return $string;
if (function_exists('iconv'))
return iconv("utf-8","gb2312//IGNORE",$string);
return mb_convert_encoding($string,'gb2312','utf-8');
}
public static function conv($str)
{
$str = self::utf8_to_gbk($str);
$len = strlen($str);
$newStr = '';
for($i=0; $i$len ; ++$i)
{
$H = ord($str[$i]);
$L = ord($str[$i+1]);
//字符集非法
if ($H 0xB0 || $L 0xA1 || $H 0xF7 || $L == 0xFF)
{
$newStr .= $str[$i];
continue;
}
if ($H 0xD8)//($H = 0xB0 $H =0xD7)//查询文字在一级汉字区(16-55)
{
$W = ($H 8) | $L;
foreach(self::$FirstTable as $key=$value)
{
if ($W $value)
{
$newStr .= self::$FirstLetter[$key];
break;
}
}
}
else// if (H = 0xD8 H = 0xF7)//查询中文在二级汉字区(56-87)
$newStr .=self::$SecondTable[$H - 0xD8][$L-0xA1];
++$i;
}
return $newStr;
}
}
echo zh2py::conv('妃');//F
php汉字转拼音问题 下
你使用的模版技术吧。
模版技术实际上是把页面中所有 {?$pinyin?} 转换 为 ?php $pinyin ?对吧?
也就是把{? 转换为 ?php
把?}转换为 ? 。
有可能是你的{? 或 ?}也页面中的function {}冲突。
比如
......
return $restr;
}?
你的模版页面我没看到~所以不确定是不是这个问题。你可以换个模版标识试试。
如果在其他页面中function可以使用,说明这个function没问题,你就在模版页下手就行了。