• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php汉字转拼音的示例_php技巧

php 搞代码 4年前 (2022-01-26) 25次浏览 已收录 0个评论
<?php<BR>class Helper_Spell{<BR>    public $spellArray = array();<br><br>    static public function getArray() {<BR>        return unserialize(file_get_contents('pytable_without_tune.txt'));<BR>    }<BR>    /**<BR>     * @desc 获取字符串的首字母<BR>     * @param $string 要转换的字符串<BR>     * @param $isOne 是否取首字母<BR>     * @param $upper 是否转换为大写<BR>     * @return string<BR>     * <BR>     * 例如:getChineseFirstChar('我是作者') 首字符全部字母+小写<BR>     * return "wo"<BR>     * <BR>     * 例如:getChineseFirstChar('我是作者',true) 首字符首字母+小写<BR>     * return "w"<BR>     * <BR>     * 例如:getChineseFirstChar('我是作者',true,true) 首字符首字母+大写<BR>     * return "W"<BR>     * <BR>     * 例如:getChineseFirstChar('我是作者',false,true) 首字符全部字母+大写<BR>     * return "WO"<BR>     */<BR>    static public function getChineseFirstChar($string,$isOne=false,$upper=false) {<BR>        $spellArray = self::getArray();<BR>        $str_arr = self::utf8_str_split($string,1); //将字符串拆分成数组<br><br>        if(preg_match('/^[\x{4e00}-\x{9fa5}]+$/u',$str_arr[0])) { //判断是否是汉字<BR>            $chinese = $spellArray[$str_arr[0]];<BR>            $result = $chinese[0];<BR>        }else {<BR>            $result = $str_arr[0];<BR>        }<br><br>        $result = $isOne ? substr($result,0,1) : $result; <br><br>        return $upper?strtoupper($result):$result;<BR>    }<br><br>    /**<BR>     * @desc 将字符串转换成拼音字符串<BR>     * @param $string 汉字字符串<BR>     * @param $upper 是否大写<BR>     * @return string<BR>     * <BR>     * 例如:getChineseChar('我是作者'); 全部字符串+小写<BR>     * return "wo shi zuo zhe"<BR>     * <BR>     * 例如:getChineseChar('我是作者',true); 首字母+小写<BR>     * return "w s z z"<BR>     * <BR>     * 例如:getChineseChar('我是作者',true,true); 首字母+大写<BR>     * return "W S Z Z"<BR>     * <BR>     * 例如:getChineseChar('我是作者',false,true); 首字母+大写<BR>     * return "WO SHI ZUO ZHE"<BR>     */<BR>    static public function getChineseChar($string,$isOne=false,$upper=false) {<BR>        global $spellArray;<BR>        $str_arr = self::utf8_str_split($string,1); //将字符串拆分成数组<BR>        $result = array();<BR>        foreach($str_arr as $char)<BR>        {<BR>            if(preg_match('/^[\x{4e00}-\x{9fa5}]+$/u',$char))<BR>            {<BR>                $chinese = $spellArray[$char];<BR>                $chinese  = $chinese[0];<BR>            }else{<BR>                $chinese=$char;<BR>            }<BR>            $chinese = $isOne ? substr($chinese,0,1) : $chinese;<BR>            $result[] = $upper ? strtoupper($chinese) : $chinese;<BR>        }<BR>        return implode(' ',$result);<BR>    }<BR>    /**<BR>     * @desc 将字符串转换成数组<BR>     * @param $str 要转换的数组<BR>     * @param $split_len<BR>     * @return array<BR>     */<BR>    private function utf8_str_split($str,$split_<i style="color:transparent">本文来源gaodai$ma#com搞$$代**码)网8</i><strong>搞代gaodaima码</strong>len=1) {</P><P>        if(!preg_match('/^[0-9]+$/', $split_len) || $split_len < 1) {<BR>            return FALSE;<BR>        }<br><br>        $len = mb_strlen($str, 'UTF-8');<br><br>        if ($len <= $split_len) {<BR>            return array($str);<BR>        }<BR>        preg_match_all('/.{'.$split_len.'}|[^\x00]{1,'.$split_len.'}$/us', $str, $ar);</P><P>        return $ar[0];<BR>    }<BR>}<BR>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php汉字转拼音的示例_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址