当前位置:Gxlcms > PHP教程 > php汉字转拼音(dede)

php汉字转拼音(dede)

时间:2021-07-01 10:21:17 帮助过:4人阅读

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 < 2) {
  17. return $str;
  18. }
  19. $fp = fopen(dirname(__FILE__).'/pinyin.dat', 'r');
  20. while (!feof($fp)) {
  21. $line = trim(fgets($fp));
  22. $pinyins[$line[0] . $line[1]] = substr($line, 3, strlen($line) - 3);
  23. }
  24. fclose($fp);
  25. for ($i = 0; $i < $slen; $i++) {
  26. if (ord($str[$i]) > 0x80) {
  27. $c = $str[$i] . $str[$i + 1];
  28. $i++;
  29. if (isset($pinyins[$c])) {
  30. if ($ishead == 0) {
  31. $restr .= $pinyins[$c];
  32. } else {
  33. $restr .= $pinyins[$c][0];
  34. }
  35. } else {
  36. $restr .= "_";
  37. }
  38. } else if (preg_match("/[a-z0-9]/i", $str[$i])) {
  39. $restr .= $str[$i];
  40. } else {
  41. $restr .= "_";
  42. }
  43. }
  44. return $restr;
  45. }

人气教程排行