示例#1
0
 private static function get_tag_name_pinyin($tag)
 {
     $cache_group_id = 'tag-name-pinyin';
     $cache = wp_cache_get($tag['term_id'], $cache_group_id);
     if ($cache) {
         return $cache;
     }
     /** 提取首字母 */
     $first_letter_pattern = '/^[a-z]/i';
     $first_letter = strtolower(mb_substr($tag['name'], 0, 1));
     preg_match($first_letter_pattern, $first_letter, $matches);
     /**
      * 存在字母开头的标签
      */
     if (!empty($matches[0])) {
         wp_cache_set($tag['term_id'], $first_letter, $cache_group_id);
         return $first_letter;
     }
     /**
      * 标签是中文开头
      */
     $utf8_tagname = mb_convert_encoding($tag['name'], 'utf-8', 'ascii,gb2312,gbk,utf-8');
     preg_match("/^[\\x{4e00}-\\x{9fa5}]/u", $utf8_tagname, $matches);
     /**
      * 不是中文,跳到下一个
      */
     if (empty($matches[0])) {
         wp_cache_set($tag['term_id'], false, $cache_group_id);
         return $first_letter;
     }
     if (!class_exists('Overtrue\\Pinyin\\Pinyin')) {
         include __DIR__ . '/inc/Pinyin/Pinyin.php';
     }
     $double_pinyins = ['zh', 'ch', 'sh', 'ou', 'ai', 'ang', 'an'];
     $tag_pinyin = Overtrue\Pinyin\Pinyin::pinyin($tag['name']);
     $tag_two_pinyin = strtolower(substr($tag_pinyin, 0, 2));
     /**
      * 判断巧舌音
      */
     if (in_array($tag_two_pinyin, $double_pinyins)) {
         wp_cache_set($tag['term_id'], $tag_two_pinyin, $cache_group_id);
         return $tag_two_pinyin;
         /**
          * 单音
          */
     } else {
         $tag_one_pinyin = mb_substr($tag_pinyin, 0, 1);
         wp_cache_set($tag['term_id'], $tag_one_pinyin, $cache_group_id);
         return $tag_one_pinyin;
     }
 }