Home  >  Article  >  Backend Development  >  php 输入汉字自动带出拼音和英文

php 输入汉字自动带出拼音和英文

WBOY
WBOYOriginal
2016-06-20 12:45:121367browse

需求就是添加一个字段的时候,自动带出中文和英文,方便数据索引。
这里只贴下代码,英文用在线api,中文用类库。我觉得这个拼音类库比较好,不会出现重庆是zhongqing之类的问题,因为可以自定义添加维护。

要说明的是百度clientid要自己申请个。调用次数的说明请看百度翻译文档

<?phpinclude('./Cn2pinyin.php');$str="扔掉";$json=file_get_contents('http://openapi.baidu.com/public/2.0/bmt/translate?client_id=yourbaiduclinetid&q='.$str.'&from=auto&to=auto');$json = stripslashes($json);$arr=json_decode($json,true);// 获取英文(需要在线,至于不在线,如果英文只是想作为一个标识或者索引字段的话,可以用插入id+拼音缩写构件唯一id)echo $eng=strtolower(str_replace(' ', '_',$arr['trans_result'][0]['dst']))."<br>";$py=new Comm_Cn2pinyin();//获取拼音 echo strtolower($py->getPinyin($str));// 获取拼音缩写echo strtolower($py->getShortPinyin($str));

拼音类库百度网盘下载地址

话说没有添加附件功能。。。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn