PHP中文汉字转换拼音的简单方法,下面方法比较简单,直接复制粘贴就可以使用
='5.0') ? array_combine($_TDataKey, $_TDataValue) : $this->_Array_Combine($_TDataKey, $_TDataValue); arsort($_Data); reset($_Data); if($_Code != 'gb2312') $_String = $this->_U2_Utf8_Gb($_String); $_Res = ''; for($i=0; $i160) { $_Q = ord(substr($_String, ++$i, 1)); $_P = $_P*256 + $_Q - 65536; } $_Res .= $this->_Pinyin($_P, $_Data); } return preg_replace("/[^a-z0-9]*/", '', $_Res); } public function _Pinyin($_Num, $_Data) { if ($_Num>0 && $_Num-10247) return ''; else{ foreach($_Data as $k=>$v){ if($v>6); $_String .= chr(0x80 | $_C & 0x3F); }elseif($_C >12); $_String .= chr(0x80 | $_C>>6 & 0x3F); $_String .= chr(0x80 | $_C & 0x3F); }elseif($_C >18); $_String .= chr(0x80 | $_C>>12 & 0x3F); $_String .= chr(0x80 | $_C>>6 & 0x3F); $_String .= chr(0x80 | $_C & 0x3F); } return iconv('UTF-8', 'GB2312', $_String); } public function _Array_Combine($_Arr1, $_Arr2) { for($i=0; $i
使用方法:
public function index(){ $pinyin=$this->Pinyin($hanzi,1); //用法:第二个参数留空则为gb1232编码 //echo $this->Pinyin('溜走网-Www.LiuZou.Com',1); //第二个参数随意设置则为utf-8编码 //echo $this->Pinyin('溜走网',1); }Thinkphp框架使用建议
把上面的方法放到一个model里面
在控制器直接调用这个model中的transform方法即可
$pinyin = new PinyinModel(); echo $test = $pinyin->transform('PHP博客',1);输出:boke
我们发现上面的输出没有PHP,也就是自动过滤掉了字母了。
© 版权声明
THE END
暂无评论内容