php漢字轉拼音(dede)

來源:互聯網
上載者:User
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. }
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.