VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • 分享一段PHP制作的中文拼音首字母工具类

这篇文章主要介绍了分享一段PHP制作的中文拼音首字母工具类的代码,非常的实用,推荐给有相同需求的童鞋们。

代码很简单,这里就不多BB了,大家看注释吧,注释都看不懂的小伙伴,求放过PHP!!!代码如下:

  1. <?php 
  2. /** 
  3. * 汉字拼音首字母工具类 
  4. *  注: 英文的字串:不变返回(包括数字)    eg .abc123 => abc123 
  5. *      中文字符串:返回拼音首字符        eg. 测试字符串 => CSZFC 
  6. *      中英混合串: 返回拼音首字符和英文   eg. 我i我j => WIWJ 
  7. *  eg. 
  8. *  $py = new str2PY(); 
  9. *  
  10. *  $result = $py->getInitials('周杰伦'); 
  11. * 
  12. *  //获取首字母 
  13. *  $result = $py->getFirstString('abc');  //A 
  14. *  $resutl = $py->getFirstString("周杰伦"); //Z 
  15. * 
  16. */ 
  17. class str2py 
  18. { 
  19.     private $_pinyins = array( 
  20. => 'A', 
  21. => 'B', 
  22. => 'C', 
  23. => 'D', 
  24. => 'E', 
  25. => 'F', 
  26. => 'G', 
  27. => 'H', 
  28. => 'J', 
  29. => 'K', 
  30. => 'L', 
  31. => 'M', 
  32. => 'N', 
  33. => 'O', 
  34. => 'P', 
  35. => 'Q', 
  36. => 'R', 
  37. => 'S', 
  38. => 'T', 
  39. => 'W', 
  40. => 'X', 
  41. => 'Y', 
  42. => 'Z', 
  43.     ); 
  44.     private $_charset = null; 
  45.     /** 
  46.      * 构造函数, 指定需要的编码 default: utf-8 
  47.      * 支持utf-8, gb2312 
  48.      * 
  49.      * @param unknown_type $charset 
  50.      */ 
  51.     public function __construct($charset = 'utf-8') 
  52.     { 
  53.         $this->_charset = $charset; 
  54.     } 
  55.     /** 
  56.      * 中文字符串 substr 
  57.      * 
  58.      * @param string $str 
  59.      * @param int    $start 
  60.      * @param int    $len 
  61.      * @return string 
  62.      */ 
  63.     private function _msubstr($str, $start, $len) 
  64.     { 
  65.         $start = $start * 2; 
  66.         $len = $len * 2; 
  67.         $strlen = strlen($str); 
  68.         $result = ''; 
  69.         for ($i = 0; $i < $strlen; $i++) 
  70.         { 
  71.             if ($i >= $start && $i < ($start + $len)) 
  72.             { 
  73.                 if (ord(substr($str, $i, 1)) > 129) 
  74.                 { 
  75.                     $result .= substr($str, $i, 2); 
  76.                 } 
  77.                 else 
  78.                 { 
  79.                     $result .= substr($str, $i, 1); 
  80.                 } 
  81.             } 
  82.             if (ord(substr($str, $i, 1)) > 129) 
  83.             { 
  84.                 $i++; 
  85.             } 
  86.         } 
  87.         return $result; 
  88.     } 
  89.     /** 
  90.      * 字符串切分为数组 (汉字或者一个字符为单位) 
  91.      * 
  92.      * @param string $str 
  93.      * @return array 
  94.      */ 
  95.     private function _cutWord($str) 
  96.     { 
  97.         $words = array(); 
  98.         while ($str != "") 
  99.         { 
  100.             if ($this->_isAscii($str)) 
  101.             {/* 非中文 */ 
  102.                 $words[] = $str[0]; 
  103.                 $str = substr($str, strlen($str[0])); 
  104.             } 
  105.             else 
  106.             { 
  107.                 $word = $this->_msubstr($str, 0, 1); 
  108.                 $words[] = $word; 
  109.                 $str = substr($str, strlen($word)); 
  110.             } 
  111.         } 
  112.         return $words; 
  113.     } 
  114.     /** 
  115.      * 判断字符是否是ascii字符 
  116.      * 
  117.      * @param string $char 
  118.      * @return bool 
  119.      */ 
  120.     private function _isAscii($char) 
  121.     { 
  122.         return ( ord(substr($char, 0, 1)) < 160 ); 
  123.     } 
  124.     /** 
  125.      * 判断字符串前3个字符是否是ascii字符 
  126.      * 
  127.      * @param string $str 
  128.      * @return bool 
  129.      */ 
  130.     private function _isAsciis($str) 
  131.     { 
  132.         $len = strlen($str) >= 3 ? 3 : 2; 
  133.         $chars = array(); 
  134.         for ($i = 1; $i < $len - 1; $i++) 
  135.         { 
  136.             $chars[] = $this->_isAscii($str[$i]) ? 'yes' : 'no'; 
  137.         } 
  138.         $result = array_count_values($chars); 
  139.         if (emptyempty($result['no'])) 
  140.         { 
  141.             return true; 
  142.         } 
  143.         return false; 
  144.     } 
  145.     /** 
  146.      * 获取中文字串的拼音首字符 
  147.      * 
  148.      * @param string $str 
  149.      * @return string 
  150.      */ 
  151.     public function getInitials($str) 
  152.     { 
  153.         if (emptyempty($str)) 
  154.             return ''; 
  155.         if ($this->_isAscii($str[0]) && $this->_isAsciis($str)) 
  156.         { 
  157.             return $str; 
  158.         } 
  159.         $result = array(); 
  160.         if ($this->_charset == 'utf-8') 
  161.         { 
  162.             $str = iconv('utf-8', 'gb2312', $str); 
  163.         } 
  164.         $words = $this->_cutWord($str); 
  165.         foreach ($words as $word) 
  166.         { 
  167.             if ($this->_isAscii($word)) 
  168.             {/* 非中文 */ 
  169.                 $result[] = $word; 
  170.                 continue; 
  171.             } 
  172.             $code = ord(substr($word, 0, 1)) * 1000 + ord(substr($word, 1, 1)); 
  173.             /* 获取拼音首字母A--Z */ 
  174.             if (($i = $this->_search($code)) != -1) 
  175.             { 
  176.                 $result[] = $this->_pinyins[$i]; 
  177.             } 
  178.         } 
  179.         return strtoupper(implode('', $result)); 
  180.     } 
  181.     /** 
  182.      *  20140624 wangtianbao 获取首字母 
  183.      *  @param string $str 
  184.      *  @return string 
  185.      */ 
  186.     public function getFirstString($str) 
  187.     { 
  188.         //先把中文转换成字母 
  189.         $new_string = $this->getInitials($str); 
  190.         if (emptyempty($new_string)) 
  191.         { 
  192.             return ''; 
  193.         } 
  194.         else 
  195.         { 
  196.             return strtoupper(substr($new_string, 0, 1)); 
  197.         } 
  198.     } 
  199.     private function _getChar($ascii) 
  200.     { 
  201.         if ($ascii >= 48 && $ascii <= 57) 
  202.         { 
  203.             return chr($ascii);  /* 数字 */ 
  204.         } 
  205.         elseif ($ascii >= 65 && $ascii <= 90) 
  206.         { 
  207.             return chr($ascii);   /* A--Z */ 
  208.         } 
  209.         elseif ($ascii >= 97 && $ascii <= 122) 
  210.         { 
  211.             return chr($ascii - 32); /* a--z */ 
  212.         } 
  213.         else 
  214.         { 
  215.             return '-'; /* 其他 */ 
  216.         } 
  217.     } 
  218.     /** 
  219.      * 查找需要的汉字内码(gb2312) 对应的拼音字符( 二分法 ) 
  220.      * 
  221.      * @param int $code 
  222.      * @return int 
  223.      */ 
  224.     private function _search($code) 
  225.     { 
  226.         $data = array_keys($this->_pinyins); 
  227.         $lower = 0; 
  228.         $upper = sizeof($data) - 1; 
  229.         $middle = (int) round(($lower + $upper) / 2); 
  230.         if ($code < $data[0]) 
  231.             return -1; 
  232.         for (;;) 
  233.         { 
  234.             if ($lower > $upper) 
  235.             { 
  236.                 return $data[$lower - 1]; 
  237.             } 
  238.             $tmp = (int) round(($lower + $upper) / 2); 
  239.             if (!isset($data[$tmp])) 
  240.             { 
  241.                 return $data[$middle]; 
  242.             } 
  243.             else 
  244.             { 
  245.                 $middle = $tmp; 
  246.             } 
  247.             if ($data[$middle] < $code) 
  248.             { 
  249.                 $lower = (int) $middle + 1; 
  250.             } 
  251.             else if ($data[$middle] == $code) 
  252.             { 
  253.                 return $data[$middle]; 
  254.             } 
  255.             else 
  256.             { 
  257.                 $upper = (int) $middle - 1; 
  258.             } 
  259.         } 
  260.     } 
  261. } 

取汉字首字母算是目前几乎没个项目中都需要用到的功能了,这里给大家推荐的是效率比较高的代码,也是在本人项目中使用的,小伙伴们如发现问题,还请留言,大家共同进步。

 



出处:http://www.phpfensi.com/php/20210503/14825.html


相关教程