Convert Chinese characters to Pinyin (dede) in php
Release: 2016-07-25 08:47:36
Original
1190 people have browsed it
Convert Chinese characters to Pinyin (dede) in php Pull an external link! http://www.tao11.cn/a0b923820dcc509a.html http://www.tao11.cn/9d4c2f636f067f89.html http://www.tao11.cn/4b5ce2fe28308fd9.html http://www.tao11 .cn/bbce2345d7772b06.html The pinyin.dat inside is nearby
- /**
- * Chinese characters
- * @param string $str String to be converted
- * @param string $charset String encoding
- * @param bool $ishead Whether to extract only the first letter
- * @return string Return result
- */
- static function GetPinyin($str,$charset="utf-8",$ishead = 0) {
- $restr = '';
- $str = trim($str );
- if($charset=="utf-8"){
- $str=iconv("utf-8","gb2312",$str);
- }
- $slen = strlen($str);
- $ pinyins=array();
- if ($slen < 2) {
- return $str;
- }
- $fp = fopen(dirname(__FILE__).'/pinyin.dat', 'r');
- while (! feof($fp)) {
- $line = trim(fgets($fp));
- $pinyins[$line[0] . $line[1]] = substr($line, 3, strlen($line) - 3);
- }
- fclose($fp);
-
- for ($i = 0; $i < $slen; $i++) {
- if (ord($str[$i]) > 0x80) {
- $c = $str[$i] . $str[$i + 1];
- $i++;
- if (isset($pinyins[$c])) {
- if ($ishead == 0) {
- $restr .= $pinyins[$c];
- } else {
- $restr .= $pinyins[$c][0];
- }
- } else {
- $restr .= "_";
- }
- } else if ( preg_match("/[a-z0-9]/i", $str[$i])) {
- $restr .= $str[$i];
- } else {
- $restr .= "_";
- }
- }
- return $restr;
- }
Copy code
|
Statement of this Website
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
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31