最近捣鼓了通过调高德地图行政区划api来获取城市信息的小demo,但做的项目有一个功能要求通过城市首字母区分城市的功能
类似于这样:
首先我想的是通过PHP来解决
实现原理是获取汉字的ASCII码通过对比判断然后返回汉字的首字母
获取首字母代码如下:
function getFirstCharter($str)
{
$str = mb_convert_encoding($str, 'gb2312','UTF-8');// 如果程序是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;
}
}
最后检查数据结果有很多的数据首字母获取为空:
例如(展示部分)发现这种方法并不是很好 很多生僻一点的字的首字母并没有获取到
SO,只好放弃另谋他路
终于在GitHub上看到了这个扩展包,Github地址:https://github.com/overtrue/pinyin
安装
使用 Composer 安装:
composer require "overtrue/pinyin:~4.0"
使用
- 内存型,适用于服务器内存空间较富余,优点:转换快
- 小内存型(默认),适用于内存比较紧张的环境,优点:占用内存小,转换不如内存型快
- I/O型,适用于虚拟机,内存限制比较严格环境。优点:非常微小内存消耗。缺点:转换慢,不如内存型转换快,php >= 5.5
可用选项:
PINYIN_TONE UNICODE 式音调:měi hǎo
PINYIN_ASCII_TONE 带数字式音调:mei3 hao3
PINYIN_NO_TONE 无音调:mei hao
PINYIN_KEEP_NUMBER 保留数字
PINYIN_KEEP_ENGLISH 保留英文
PINYIN_KEEP_PUNCTUATION 保留标点
PINYIN_UMLAUT_V 使用v代替yu, 例如:吕lyu将会转为lv
示例:
use Overtrue\Pinyin\Pinyin;
// 小内存型
$pinyin = new Pinyin(); // 默认
// 内存型
// $pinyin = new Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
// I/O型
// $pinyin = new Pinyin('Overtrue\Pinyin\GeneratorFileDictLoader');
//返回拼音数组形式
$pinyin->convert('带着希望去旅行,比到达终点更美好');
// ["dai", "zhe", "xi", "wang", "qu", "lyu", "xing", "bi", "dao", "da", "zhong", "dian", "geng", "mei", "hao"]
//返回拼音调式拼音数组
$pinyin->convert('带着希望去旅行,比到达终点更美好', PINYIN_TONE);
// ["dài","zhe","xī","wàng","qù","lǚ","xíng","bǐ","dào","dá","zhōng","diǎn","gèng","měi","hǎo"]
//返回带数字音调数组
$pinyin->convert('带着希望去旅行,比到达终点更美好', PINYIN_ASCII_TONE);
//["dai4","zhe","xi1","wang4","qu4","lyu3","xing2","bi3","dao4","da2","zhong1","dian3","geng4","mei3","hao3"]
//生成用于链接的拼音字符串
$pinyin->permalink('带着希望去旅行'); // dai-zhe-xi-wang-qu-lyu-xing
$pinyin->permalink('带着希望去旅行', '.'); // dai.zhe.xi.wang.qu.lyu.xing
//返回首字符字符串
$pinyin->abbr('带着希望去旅行'); // dzxwqlx
$pinyin->abbr('带着希望去旅行', '-'); // d-z-x-w-q-l-x
$pinyin->abbr('你好2018!', PINYIN_KEEP_NUMBER); // nh2018
$pinyin->abbr('Happy New Year! 2018!', PINYIN_KEEP_ENGLISH); // HNY2018
//翻译整段文字为拼音
$pinyin->sentence('带着希望去旅行,比到达终点更美好!');
// dai zhe xi wang qu lyu xing, bi dao da zhong dian geng mei hao!
$pinyin->sentence('带着希望去旅行,比到达终点更美好!', PINYIN_TONE);
// dài zhe xī wàng qù lǚ xíng, bǐ dào dá zhōng diǎn gèng měi hǎo!
//翻译姓名
$pinyin->name('单某某'); // ['shan', 'mou', 'mou']
$pinyin->name('单某某', PINYIN_TONE); // ["shàn","mǒu","mǒu"]
实战:
生僻字获取
首先我从百度上找了一些生僻字
猋(biao)麤(cu)掱(pa)赑(bi)焺(sheng)翾(xuan)
$pinyin = new Pinyin();
//输出带连接符的拼音字符串
echo $pinyin->permalink('猋麤掱赑焺翾');
echo "<br>";
//输出首字母字符串
echo $pinyin->abbr('猋麤掱赑焺翾');
运行发现生僻字转拼音无压力 借此将这个扩展包推荐给大家
非特殊说明,本博所有文章均为博主原创。
如若转载,无需和我联系,但请注明出处:https://www.yangpanyao.com/archives/53.html