PHP将字符切割成每个拼音

假设我们有个字符串是拼音形式的,比如“woaini”,我们需要将他切割成每一个拼音的组合如“wo-ai-ni”。
方法如下:

    $str = 'woaini';
    $py = new py();
    $result = $py->analyze($str);
echo "<pre>";print_r($result);echo "<pre>";



class py
{
    private $pinyinRes = [];
    /**
     * 分析
     *
     * @param string $str
     * @return string
     * @throws Exception
     */
    public function analyze($str)
    {
            $this->pinyin($str);
            return $this->pinyinRes;
    }

    /**
     * @desc将字符串切割成各个拼音组成的集合(以‘_’分隔字符串)
     * @param $str 需要切割的字符串
     * @param array $group 全体拼音的分组集合
     * @param string $partStr 父级已经切割过的字符串
     * @return bool
     */
    public function pinyin($str,$group = [],$partStr=''){
        // 分组
        if (empty($group)) {
            $pinyinConfig = [
                'a','ai','an','ao','ba','bai','ban','bang','bao','bei','ben','beng','bi','bian','biao','bie','bin','bing','bo','bu','ca','cai','can','cang','cao','ce','cen','ceng','cha','chai','chan','chang','chao','che','chen','cheng','chi','chong','chou','chu','chua','chuai','chuan','chuang','chui','chun','chuo','ci','cong','cou','cu','cuan','cui','cun','cuo','da','dai','dan','dang','dao','de','dei','den','deng','di','dia','dian','diao','die','ding','diu','dong','dou','du','duan','dui','dun','duo','e','en','eng','er','fa','fan','fang','fei','fen','feng','fiao','fo','fou','fu','ga','gai','gan','gang','gao','ge','gei','gen','geng','gong','gou','gu','gua','guai','guan','guang','gui','gun','guo','ha','hai','han','hang','hao','he','hei','hen','heng','hong','hou','hu','hua','huai','huan','huang','hui','hun','huo','ji','jia','jian','jiang','jiao','jie','jin','jing','jiong','jiu','ju','juan','jue','ka','kai','kan','kang','kao','ke','ken','keng','kong','kou','ku','kua','kuai','kuan','kuang','kui','kun','kuo','la','lai','lan','lang','lao','le','lei','leng','li','lia','lian','liang','liao','lie','lin','ling','liu','lo','long','lou','lu','luan','lun','luo','lv','lve','ma','mai','man','mang','mao','me','mei','men','meng','mi','mian','miao','mie','min','ming','miu','mo','mou','mu','na','nai','nan','nang','nao','ne','nei','nen','neng','ni','nian','niang','niao','nie','nin','ning','niu','nong','nou','nu','nuan','nun','nuo','nv','nve','o','ou','pa','pai','pan','pang','pao','pei','pen','peng','pi','pian','piao','pie','pin','ping','po','pou','pu','qi','qia','qian','qiang','qiao','qie','qin','qing','qiong','qiu','qu','quan','que','qun','ran','rang','rao','re','ren','reng','ri','rong','rou','ru','rua','ruan','rui','run','ruo','sa','sai','san','sang','sao','se','sen','seng','sha','shai','shan','shang','shao','she','shei','shen','sheng','shi','shou','shu','shua','shuai','shuan','shuang','shui','shun','shuo','si','song','sou','su','suan','sui','sun','suo','ta','tai','tan','tang','tao','te','tei','teng','ti','tian','tiao','tie','ting','tong','tou','tu','tuan','tui','tun','tuo','wa','wai','wan','wang','wei','wen','weng','wo','wu','xi','xia','xian','xiang','xiao','xie','xin','xing','xiong','xiu','xu','xuan','xue','xun','ya','yan','yang','yao','ye','yi','yin','ying','yo','yong','you','yu','yuan','yue','yun','za','zai','zan','zang','zao','ze','zei','zen','zeng','zha','zhai','zhan','zhang','zhao','zhe','zhei','zhen','zheng','zhi','zhong','zhou','zhu','zhua','zhuai','zhuan','zhuang','zhui','zhun','zhuo','zi','zong','zou','zu','zuan','zui','zun','zuo'
            ];
            foreach ($pinyinConfig as $c) {
                $group[$c[0]][] = $c;
            }
        }
        // 首字不符
        if (!array_key_exists($str[0], $group)) {
            return false;
        }

        $length      = strlen($str);
        for ($i = 1; $i <=6; $i++){//拼音最多6个字符长度
            $start = 0;
            if(!empty($partStr)){
                $start = $this->getPartLen($partStr);
            }
            if($start+$i>$length){
                continue;
            }
            $part   = substr($str, $start, $i);

            $first       = $str[$start];
            $inGroup     = $group[$first];//所在分组
            $inGroupKey  = array_flip($inGroup);//该字母所在组别

            $subLen = 0;
            if (array_key_exists($part, $inGroupKey)) {
                $subLen = strlen($part);
            }
            if($subLen){
                if(!empty($partStr)){
                    $nowPartStr = $partStr.'_'.$part;
                }else{
                    $nowPartStr = $part;
                }
                $nowLen = $this->getPartLen($nowPartStr);
                if($nowLen<$length){
                    $this->pinyin($str, $group,$nowPartStr);
                }elseif($nowLen==$length){
                    $this->pinyinRes[] =$nowPartStr;
                }
            }
        }
    }

    /**
     * 获取已切割的字符串长度
     * @param $partStr
     * @return int
     */
    public function getPartLen($partStr){
        if(empty($partStr)) return 0;
        $partArr = explode('_',$partStr);
        $len = 0;
        foreach ($partArr as $str){
            $len += strlen($str);
        }
        return $len;
    }




}

打印结果
在这里插入图片描述
以 xianguang 测试,结果为(有两种可能):
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值