Heim  >  Artikel  >  Backend-Entwicklung  >  php汉字转拼音(dede)

php汉字转拼音(dede)

WBOY
WBOYOriginal
2016-07-25 08:47:361112Durchsuche
php汉字转拼音(dede)
拉个外链哈!
http://www.tao11.cn/a0b923820dcc509a.html
http://www.tao11.cn/9d4c2f636f067f89.html
http://www.tao11.cn/4b5ce2fe28308fd9.html
http://www.tao11.cn/bbce2345d7772b06.html
里面的pinyin.dat 在附近里面
  1. /**
  2. * 汉字
  3. * @param string $str 待转换的字符串
  4. * @param string $charset 字符串编码
  5. * @param bool $ishead 是否只提取首字母
  6. * @return string 返回结果
  7. */
  8. static function GetPinyin($str,$charset="utf-8",$ishead = 0) {
  9. $restr = '';
  10. $str = trim($str);
  11. if($charset=="utf-8"){
  12. $str=iconv("utf-8","gb2312",$str);
  13. }
  14. $slen = strlen($str);
  15. $pinyins=array();
  16. if ($slen return $str;
  17. }
  18. $fp = fopen(dirname(__FILE__).'/pinyin.dat', 'r');
  19. while (!feof($fp)) {
  20. $line = trim(fgets($fp));
  21. $pinyins[$line[0] . $line[1]] = substr($line, 3, strlen($line) - 3);
  22. }
  23. fclose($fp);
  24. for ($i = 0; $i if (ord($str[$i]) > 0x80) {
  25. $c = $str[$i] . $str[$i + 1];
  26. $i++;
  27. if (isset($pinyins[$c])) {
  28. if ($ishead == 0) {
  29. $restr .= $pinyins[$c];
  30. } else {
  31. $restr .= $pinyins[$c][0];
  32. }
  33. } else {
  34. $restr .= "_";
  35. }
  36. } else if (preg_match("/[a-z0-9]/i", $str[$i])) {
  37. $restr .= $str[$i];
  38. } else {
  39. $restr .= "_";
  40. }
  41. }
  42. return $restr;
  43. }
复制代码


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