PHP中文转拼音工具:Pinyin

jopen 10年前

Pinyin 是一个将中文转换为拼音的PHP工具,支持多音字,以及10w+常用词的正确音译,基于CC-CEDICT (http://www.mdbg.net/chindict/chindict.php?page=cedict) 词典。

功能:

  • 10w+词典词库支持,支持更新词库

  • 自定义分割符

  • 可选是否输出声调

Install

  1. installing via Composer:

    Next, update your project's composer.json file to include Pinyin:

    {     "require": {         "joychao/pinyin": "~1.0"     } } 
  2. download source file src/Pinyin.php and include it.

Usage

<?php  echo Pinyin::trans('带着希望去旅行,比到达终点更美好');    // dài zhe xī wàng qù lǔ xíng bǐ dào dá zhōng diǎn gèng měi hǎo 

Setting

  • delimiter Separator between each pinyin, default is a space ' '.
  • accent Whether the output tone.

<?php  $setting = [              'delimiter' => '-',             ];  Pinyin::set($setting);  echo Pinyin::trans('带着希望去旅行,比到达终点更美好');    // dài-zhe-xī-wàng-qù-lǔ-xíng-bǐ-dào-dá-zhōng-diǎn-gèng-měi-hǎo

    <?php  $setting = [              'delimiter' => '-',              'accent' => false,             ];    echo Pinyin::trans('带着希望去旅行,比到达终点更美好', $setting);    // dai-zhe-xi-wang-qu-lu-xing-bi-dao-da-zhong-dian-geng-mei-hao
    <?php  $setting = [              'accent' => false,             ];  Pinyin::set($setting);  echo Pinyin::trans('带着希望去旅行,比到达终点更美好');    // dai zhe xi wang qu lu xing bi dao da zhong dian geng mei hao

    项目主页:http://www.open-open.com/lib/view/home/1405498265000