Heim  >  Artikel  >  Backend-Entwicklung  >  将数据库中地区表 地区中文名字->英文(3千多条数据)

将数据库中地区表 地区中文名字->英文(3千多条数据)

WBOY
WBOYOriginal
2016-08-30 09:36:43882Durchsuche

我现在数据库地区表里面有3千多条数据,现在的需求是将每一行记录中的地区名称转化为英文(就是汉语拼音),同时保存起来,附上图更直观一点

将数据库中地区表 地区中文名字->英文(3千多条数据)

我想请问,有没有汉语的语言库,我自己写一个PHP程序,将汉语转化为拼音,然后通过sql语句批量进行插入,总不能一行行去改呀。
大家有什么办法??请解答

回复内容:

我现在数据库地区表里面有3千多条数据,现在的需求是将每一行记录中的地区名称转化为英文(就是汉语拼音),同时保存起来,附上图更直观一点

将数据库中地区表 地区中文名字->英文(3千多条数据)

我想请问,有没有汉语的语言库,我自己写一个PHP程序,将汉语转化为拼音,然后通过sql语句批量进行插入,总不能一行行去改呀。
大家有什么办法??请解答

<code>1、项目下需放入pinyin4j-2.5.0.jar,可再网上下载,这里不提供下载地址了

2、代码:
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**  
 * 汉字转换位汉语拼音,英文字符不变  
 *  
 */  
public class Cn2Spell {   
    
    /**  
    * 汉字转换位汉语拼音首字母,英文字符不变  
    * @param chines 汉字  
    * @return 拼音  
    */  
    public static String converterToFirstSpell(String chines){          
        String pinyinName = "";   
        char[] nameChar = chines.toCharArray();   
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();   
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);   
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);   
        for (int i = 0; i  128) {   
                try {   
                    pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0].charAt(0);   
                } catch (BadHanyuPinyinOutputFormatCombination e) {   
                    e.printStackTrace();   
                }   
            }else{   
                pinyinName += nameChar[i];   
            }   
        }   
        return pinyinName;   
    }   
    
    /**  
    * 汉字转换位汉语拼音,英文字符不变  
    * @param chines 汉字  
    * @return 拼音  
    */  
    public static String converterToSpell(String chines){           
        String pinyinName = "";   
        char[] nameChar = chines.toCharArray();   
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();   
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);   
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);   
        for (int i = 0; i  128) {   
                try {   
                    pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0];   
                } catch (BadHanyuPinyinOutputFormatCombination e) {   
                    e.printStackTrace();   
                }   
            }else{   
                pinyinName += nameChar[i];   
            }   
        }   
        return pinyinName;   
    }   
       
    public static void main(String[] args) {   
        System.out.println(converterToFirstSpell("欢迎来到Java世界"));   
        System.out.println(converterToSpell("欢迎来到Java世界"));  
    }   
}  </code>

https://github.com/overtrue/p...

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn