今天產品下了新需求,要給系統加雙語切換,在做到國家城市庫映射的時候,我在度娘上找了個雙語版的資料庫,可是系統已經在生產半年了,直接換表肯定是要背鍋的,轉念一想,要是能把城市資訊直接替換為拼音不就行了,在此將操作步驟整理下來,
首先需要安裝一下擴展overtrue/pinyin,執行命令,我的框架是6.0,安裝的擴展版本是^4.0
composer require overtrue/pinyin -vvv
GitHub地址:https://github.com/overtrue/pinyin
在控制器里引入
use \Overtrue\Pinyin\Pinyin;
public function index(){
$list = \App\Model\City::whereNull('name_en')->get(); // 取出沒有英文的資料
$pinyin = new Pinyin();
foreach ($list as $item){
$arr = $pinyin->permalink($item['name'],''); // 中文轉拼音
$en = str_replace(['qu','xian','shi'],'',$en); //替換掉區縣市后綴
$item->name_en = ucfirst($en);
$item->save();
}
}
overtrue/pinyin擴展的其他實作:
- 漢字轉成拼音
$pinyin->convert('保護地球,熱愛和平');
//['bao','hu','dd','qiu','re','ai','he','ping']
- 漢字轉成帶音調的拼音
$pinyin->convert('保護地球,熱愛和平', PINYIN_TONE);
//['bǎo','hù','dì','qiú','rè','ài','hé','píng']
- 漢字轉成拼音字串
$pinyin->permalink('保護地球,熱愛和平');
//baohudiqiureaiheping
- 漢字轉成拼音首字母字串
$pinyin->abbr('保護地球,熱愛和平');
//bhdqrahp
$pinyin->abbr('保護地球,熱愛和平',',');
//b,h,d,q,r,a,h,p
- 轉成拼音時包含符號
$pinyin->sentence('保護地球,熱愛和平');
//bao hu di qiu, re ai he ping
$pinyin->sentence('保護地球,熱愛和平','-');
//bao-hu-di-qiu,-re-ai-he-ping
- 轉換帶多音字的姓名
$pinyin->name('仇笑癡');
//['qiu','xiao','chi']
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/492231.html
標籤:PHP
上一篇:laravel框架(完整上傳到資料庫,不提交圖片)(以提交員工資訊為例)
下一篇:phpcurl函式類模擬Curl get post header refer攜帶Cookie模擬訪問來源Refer模擬UseaAgent
