PinYin.php 722 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace app\common\components;
  3. use Yii;
  4. use yii\base\Model;
  5. use Overtrue\Pinyin\Pinyin as py;
  6. /**
  7. * 汉字转拼音
  8. * @property integer
  9. */
  10. class PinYin extends Model
  11. {
  12. private $py;
  13. public function __construct(){
  14. $this->py = new py();
  15. }
  16. /**
  17. * 将中文编码成拼音
  18. * @param string $utf8Data utf8字符集数据
  19. * @param string $sRetFormat 返回格式 [head:首字母|all:全拼音]
  20. * @return string
  21. */
  22. public function encode($utf8Data, $sRetFormat='all'){
  23. if($sRetFormat=='all')
  24. {
  25. return $this->py->permalink($utf8Data,'');
  26. }
  27. else
  28. {
  29. return $this->py->abbr($utf8Data);
  30. }
  31. }
  32. }