汉字转拼音+简繁互转

  1. 作者QQ:67065435 QQ群:821635552

  2. 本站内容全部为作者原创,转载请注明出处!

  3. 扩展需求

    mbstring
    
  4. php文件代码

    <?php
    
    class CharService
    {
        private $pinyin_1;
        private $pinyin_2;
        private $string_j;
        private $string_f;
    
        public function __construct()
        {
            $this->pinyin_1 = "";//找作者要
            $this->pinyin_2 = "";//找作者要
            $this->string_j = "";//找作者要
            $this->string_f = "";//找作者要
        }
    
        /**
         * 简体转繁体
         * @param $str
         * @return mixed
         */
        public function jianToFan($str)
        {
            return str_replace(mb_str_split($this->string_j), mb_str_split($this->string_f), $str);
        }
    
        /**
         * 繁体转简体
         * @param $str
         * @return mixed
         */
        public function fanToJian($str)
        {
            return str_replace(mb_str_split($this->string_f), mb_str_split($this->string_j), $str);
        }
    
        /**
         * 汉字转拼音1
         * @param $str
         * @return array
         */
        public function strToPin1($str)
        {
            $arr = mb_str_split($str);
            foreach ($arr as &$s) {
                if (preg_match("/[\x{4e00}-\x{9fa5}]+/u", $s)) {
                    $pattern = "/{$s}(.*?)\,/";
                    preg_match_all($pattern, $this->pinyin_1, $matches);
                    $s = !empty($matches[1][0]) ? $matches[1][0] : $s;
                }
            }
            return $arr;
        }
    
        /**
         * 汉字转拼音2
         * @param $str
         * @return array
         */
        public function strToPin2($str)
        {
            $arr = mb_str_split($str);
            foreach ($arr as &$s) {
                if (preg_match("/[\x{4e00}-\x{9fa5}]+/u", $s)) {
                    $pattern = "/{$s}(.*?)\,/";
                    preg_match_all($pattern, $this->pinyin_2, $matches);
                    $s = !empty($matches[1][0]) ? $matches[1][0] : $s;
                }
            }
            return $arr;
        }
    }
    
Copyright © 豆包嘿嘿~ 2012-∞ 冀ICP备17033181号 all right reserved,powered by Gitbook修订: 21-07-07 12:31

results matching ""

    No results matching ""