企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 安装 `composer require "overtrue/pinyin:~4.0"` ## 使用 ~~~ 可选转换方案: - 内存型,适用于服务器内存空间较富余,优点:转换快 $type=1 - 小内存型(默认),适用于内存比较紧张的环境,优点:占用内存小,转换不如内存型快 $type=2 - I/O型,适用于虚拟机,内存限制比较严格环境。优点:非常微小内存消耗。缺点:转换慢,不如内存型转换快,php >= 5.5 $type=3 ~~~ ### 拼音数组使用 ~~~ // 小内存型 Pinyin::instance(1)->convert('带着希望去旅行,比到达终点更美好'); // ["dai", "zhe", "xi", "wang", "qu", "lyu", "xing", "bi", "dao", "da", "zhong", "dian", "geng", "mei", "hao"] Pinyin::instance(1)->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::instance(1)->convert('带着希望去旅行,比到达终点更美好', PINYIN_ASCII_TONE); //["dai4","zhe","xi1","wang4","qu4","lyu3","xing2","bi3","dao4","da2","zhong1","dian3","geng4","mei3","hao3"] ~~~ ### 生成用于链接的拼音字符串 ```php Pinyin::instance(1)->permalink('带着希望去旅行'); // dai-zhe-xi-wang-qu-lyu-xing Pinyin::instance(1)->permalink('带着希望去旅行', '.'); // dai.zhe.xi.wang.qu.lyu.xing ``` ### 获取首字符字符串 ```php Pinyin::instance(1)->abbr('带着希望去旅行'); // dzxwqlx Pinyin::instance(1)->abbr('带着希望去旅行', '-'); // d-z-x-w-q-l-x Pinyin::instance(1)->abbr('你好2018!', PINYIN_KEEP_NUMBER); // nh2018 Pinyin::instance(1)->abbr('Happy New Year! 2018!', PINYIN_KEEP_ENGLISH); // HNY2018 ``` ### 翻译整段文字为拼音 将会保留中文字符:`,。 ! ? : “ ” ‘ ’` 并替换为对应的英文符号。 ```php Pinyin::instance(1)->sentence('带着希望去旅行,比到达终点更美好!'); // dai zhe xi wang qu lyu xing, bi dao da zhong dian geng mei hao! Pinyin::instance(1)->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! ``` ### 翻译姓名 姓名的姓的读音有些与普通字不一样,比如 ‘单’ 常见的音为 `dan`,而作为姓的时候读 `shan`。 ```php Pinyin::instance(1)->name('单某某'); // ['shan', 'mou', 'mou'] Pinyin::instance(1)->name('单某某', PINYIN_TONE); // ["shàn","mǒu","mǒu"] ``` ~~~