效果:中國 =>zg
用法:$result =character::getenbycnbystring(『i是中國人』,3);//這邊的3為擷取的長度
echo $result;
結果:
**
<?php
/** * @author liaosp.top
* @time: 2018/12/4 -16:39
* @version 1.0
* @describe: 中文轉化為英文首字母 中國:zg ;中2國:zg;z國:zg
* 1:省略了數字
* 2:
* ...
*/class character
preg_match_all("/./u", $string, $arr);
$exp =$arr[0];
if(empty($exp))
$sum =1;
$res ='';
foreach ($exp as $key=>$value)
if($sum >$length)
}return $res;
}/**
* 轉化中文為字母的工具
* @param $str
* @return null|string
*/public static function getfirstcharter($str)
if (empty($str))
$fchar = ord($str);
if ($fchar >= ord('a') && $fchar <= ord('z'))
return strtoupper($str);
$s1 = iconv('utf-8', 'gb2312', $str);
$s2 = iconv('gb2312', 'utf-8', $s1);
$s = $s2 == $str ? $s1 : $str;
$asc = ord($s) * 256 + ord($s) - 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;
}}//用法
$result =character::getenbycnbystring('i是中國人',3);
echo $result;
github:
參考: php分割中文字串為陣列的簡單例子
PHP生僻漢字轉化為拼音
php 漢字轉拼音 包含20902個基本漢字 5059生僻字 author 樓教主 cik520 qq.com version v1.2 note 請開啟 mb string 擴充套件 測試用例 start time microtime 1 var dump pinyin 對多音字無能為力 var ...
中文轉拼音 中英文首字母提取
import net.sourceforge.pinyin4j.pinyinhelper import net.sourceforge.pinyin4j.format.hanyupinyincasetype import net.sourceforge.pinyin4j.format.hanyupi...
php 把數字轉化為大寫中文
1.120002129.25 轉化後 壹億貳仟萬貳仟壹佰貳拾玖圓貳角伍分 2.12000.2145 轉化後 壹萬貳仟圓貳角壹分肆釐伍毫 3.1020001211 轉化後 壹拾億貳仟萬壹仟貳佰壹拾壹圓整從小數點分割成兩部分,整數部分和小數部分分別處理。整數四個一組進行處理,用substr函式分組,然後...