|
本类实现将UTF-8中文字符转换成拼音 或者实现拼音的首字母连锁。
作用:可以实现索引,按照拼音的首字母进行升降序排列
部分代码:
PHP复制代码 function character2acsii ($str) {
$ret = "";
for($i = 0; $i < strlen ( $str ); $i ++) {
$p = ord ( substr ( $str, $i, 1 ) );
if ($p > 160) {
$q = ord ( substr ( $str, ++ $i, 1 ) );
$p = $p * 256 + $q - 65536;
}
//Without leaving any space Please delete the string----‘." "’
$ret .= $this->acsii2character ( $p ) . " ";
}
return $ret;
}
/*
* cn2py
* @access public
*/
function cn2py ($var = null) {
if (strlen ( trim ( $var ) ) > 0) {
//make UTF-8 $var to GBK
$str = iconv ( 'UTF-8', 'GBK', $var );
return $this->character2acsii ( $str );
} else {
return false;
}
}
/*
* @auth liu
* @access public
* the initial letter (of a Chinese character)
*/
function initial ($var = null) {
$pinyin = '';
$character = $this->cn2py ( $var );
if (strpos ( $character, ' ' ) > 0) {
$str = explode ( " ", $character );
foreach ( $str as $word ) {
$pinyin .= substr ( $word, 0, 1 );
}
} else {
$pinyin .= substr ( $character, 0, 1 );
}
return $pinyin;
} 复制代码
Chinese.rar
(3.4 KB, 下载次数: 172)
|
评分
-
查看全部评分
|