您的位置:首页 > 编程语言 > PHP开发

php通过拼音首字母查询数据mssql(高效版)

2013-05-03 18:01 197 查看
<?php
class pysql{
//按拼音检索
function GetHanziScope($pinyin){
$py=strtolower ($pinyin);
$hz=array();
for ($i = 0; $i < strlen($py); $i++) {
$index = substr($py,$i,1);
if($index == "a"){$hz[$i][0] = "吖";$hz[$i][1] = "驁";}
else if($index == "b"){$hz[$i][0] = "八";$hz[$i][1] = "簿";}
else if($index == "c"){$hz[$i][0] = "嚓";$hz[$i][1] = "錯";}
else if($index == "d"){$hz[$i][0] = "咑";$hz[$i][1] = "鵽";}
else if($index == "e"){$hz[$i][0] = "妸";$hz[$i][1] = "樲";}
else if($index == "f"){$hz[$i][0] = "发";$hz[$i][1] = "猤";}
else if($index == "g"){$hz[$i][0] = "旮";$hz[$i][1] = "腂";}
else if($index == "h"){$hz[$i][0] = "妎";$hz[$i][1] = "夻";}
else if($index == "j"){$hz[$i][0] = "丌";$hz[$i][1] = "攈";}
else if($index == "k"){$hz[$i][0] = "咔";$hz[$i][1] = "穒";}
else if($index == "l"){$hz[$i][0] = "垃";$hz[$i][1] = "鱳";}
else if($index == "m"){$hz[$i][0] = "嘸";$hz[$i][1] = "椧";}
else if($index == "n"){$hz[$i][0] = "拏";$hz[$i][1] = "桛";}
else if($index == "o"){$hz[$i][0] = "噢";$hz[$i][1] = "漚";}
else if($index == "p"){$hz[$i][0] = "妑";$hz[$i][1] = "曝";}
else if($index == "q"){$hz[$i][0] = "七";$hz[$i][1] = "裠";}
else if($index == "r"){$hz[$i][0] = "亽";$hz[$i][1] = "鶸";}
else if($index == "s"){$hz[$i][0] = "仨";$hz[$i][1] = "蜶";}
else if($index == "t"){$hz[$i][0] = "他";$hz[$i][1] = "籜";}
else if($index == "w"){$hz[$i][0] = "屲";$hz[$i][1] = "鶩";}
else if($index == "x"){$hz[$i][0] = "夕";$hz[$i][1] = "鑂";}
else if($index == "y"){$hz[$i][0] = "丫";$hz[$i][1] = "韻";}
else if($index == "z"){$hz[$i][0] = "帀";$hz[$i][1] = "咗";}
else {$hz[$i][0] = $index;$hz[$i][1] = $index;}
}
return $hz;
}
//按拼音检索
function GetPinyinWhereString($fieldName,$pinyin){
$pinyin_arr=$this->GetHanziScope($pinyin);
//print_r($pinyin_arr);
$whereString='(';
for ($i = 0; $i < count($pinyin_arr); $i++) {
$whereString .= "(SUBSTRING(" . $fieldName . ", " . ($i + 1) . ", 1) >= '" . $pinyin_arr[$i][0] . "' AND SUBSTRING(" . $fieldName . ", " . ($i + 1) . ", 1) <= '" . $pinyin_arr[$i][1] . "') AND ";
}
if(substr($whereString,strlen($whereString)-4,4)=='AND '){
$whereString=substr($whereString,0,strlen($whereString)-4);
}
return $whereString.')';
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐