After searching on the Internet, "PHP to Pinyin" is everywhere, but the code only supports GB2312.
Even the words "骞", "霜", and "Hui" cannot be supported.
For converting Chinese names to Pinyin, that code is far from enough.
A correct GBK to Pinyin code is now announced. Many thanks to the original author: Ma Minglian (!hightman) Home page: http://php.twomice.net
The original DEMO only supports one-word query. I modified it to support a string, both Chinese and English. The original text is returned in English, and the Chinese characters are converted into pinyin with tones.
Multi-phonetic characters are not supported for the time being. For example, "Zeng" will be converted to ceng2, but there is no zeng1.
(There is a yii-chinese PHP project on the Internet, but unfortunately the pinyin conversion is also GB2312)
How to use:
$str = 'Chinese characters abc123-=+';
$py = pinyin::instance(); //Single instance, if you like, $py = new pinyin(); also works.
echo $py->get($str);
Please download the entire code at the end of the page.
- class pinyin{
- var $_fp = false;
-
- function __construct() {
- $_dat = DISCUZ_ROOT."./source/include/table/py.dat";
- if (! $this->_fp)
- $this->_fp = fopen($_dat,'rb');
- }
-
-
-
- /**
- * return a simple instance
- * @return
- */
- function &instance() {
- static $object;
- if(empty($object)) {
- $object = new self();
- }
- return $object;
- }
- function anystring2gbk($str) {
- $encode = mb_detect_encoding($str,"ASCII,UNICODE, UTF-8,GBK,CP936,EUC-CN,BIG-5,EUC-TW");
- return ($encode != 'CP936' && $encode != 'ASCII' && $encode != 'GBK' ? iconv ($encode,'GBK',$str) : $str);
- }
- function get($str,$separator = ' ') {
- //$str = iconv('UTF-8','GBK', $str);
- $str = anystring2gbk($str); //If you can determine the input encoding, you can use the previous line of code
- $len = strlen($str);
-
- $i = 0;$result = array( );
- while ($i < $len) {
- $s = ord($str{$i});
- if ($s > 160) {
- $word = $this->word2py($s ,ord($str{++$i}));
- } else {
- $word = $str{$i};
- }
- $result[] = $word;
- ++$i;
- }
- return implode($separator,$result);
- }
-
- private function word2py($h,$l) {
- $high = $h - 0x81;
- $low = $l - 0x40;
- // Calculate offset position
- $off = ($high<<8) + $low - ($high * 0x40);
- // Determine the off value
- if ($off < 0) {
- return chr($h).chr($ l);
- }
- fseek($this->_fp, $off * 8, SEEK_SET);
- $ret = fread($this->_fp, 8);
- $ret = unpack('a8py', $ ret);
- return $ret['py'];
-
- }
-
- function __destruct() {
- if ($this->_fp)
- fclose($this->_fp);
- }
- }
-
- ?>
Copy code
|