<?php namespace app\common\components; use Yii; use yii\base\Model; use Overtrue\Pinyin\Pinyin as py; /** * 汉字转拼音 * @property integer */ class PinYin extends Model { private $py; public function __construct(){ $this->py = new py(); } /** * 将中文编码成拼音 * @param string $utf8Data utf8字符集数据 * @param string $sRetFormat 返回格式 [head:首字母|all:全拼音] * @return string */ public function encode($utf8Data, $sRetFormat='all'){ if($sRetFormat=='all') { return $this->py->permalink($utf8Data,''); } else { return $this->py->abbr($utf8Data); } } }