Home  >  Article  >  Backend Development  >  Change the region table in the database to the Chinese name of the region -> English (more than 3,000 pieces of data)

Change the region table in the database to the Chinese name of the region -> English (more than 3,000 pieces of data)

WBOY
WBOYOriginal
2016-08-30 09:36:43936browse

I currently have more than 3,000 pieces of data in the database region table. My current need is to convert the region name in each row of records into English (that is, Chinese Pinyin) and save it at the same time. The attached picture is more intuitive

Change the region table in the database to the Chinese name of the region -> English (more than 3,000 pieces of data)

I would like to ask if there is a Chinese language library. I write a PHP program myself to convert Chinese into Pinyin, and then insert it in batches through SQL statements. I can't change it line by line.
What can you do? ? Please answer

Reply content:

I currently have more than 3,000 pieces of data in the database region table. My current need is to convert the region name in each row of records into English (that is, Chinese Pinyin) and save it at the same time. The attached picture is more intuitive

Change the region table in the database to the Chinese name of the region -> English (more than 3,000 pieces of data)

I would like to ask if there is a Chinese language library. I write a PHP program myself to convert Chinese into Pinyin, and then insert it in batches through SQL statements. I can't change it line by line.
What can you do? ? Please answer

<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 < nameChar.length; i++) {   
            if (nameChar[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 < nameChar.length; i++) {   
            if (nameChar[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...

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