require_once('pinyin_table.php'); -
- function get_pinyin_array($string)
- {
- global $pinyin_table;
- $flow = array();
- for ($i=0;$i {
- if (ord($string[$i]) >= 0x81 and ord($string[$i]) <= 0xfe)
- {
- $h = ord($string[$i]);
- if (isset($string[$i+1]))
- {
- $i++;
- $l = ord($string[$i]);
- if (isset($pinyin_table[$h][$l]))
- {
- array_push($flow,$pinyin_table[$h][$l]);
- }
- else
- {
- array_push($flow,$h);
- array_push($flow,$l);
- }
- }
- else
- {
- array_push($flow,ord($string[$i]));
- }
- }
- else
- {
- array_push($flow,ord($string[$i]));
- }
- }
-
- //print_r($flow);
-
- $pinyin = array();
- $pinyin[0] = '';
- for ($i=0;$i {
- if (is_array($flow[$i]))
- {
- if (sizeof($flow[$i]) == 1)
- {
- foreach ($pinyin as $key => $value)
- {
- $pinyin[$key] .= $flow[$i][0];
- }
- }
- if (sizeof($flow[$i]) > 1)
- {
- $tmp1 = $pinyin;
- foreach ($pinyin as $key => $value)
- {
- $pinyin[$key] .= $flow[$i][0];
- }
- for ($j=1;$j {
- $tmp2 = $tmp1;
- for ($k=0;$k {
- $tmp2[$k] .= $flow[$i][$j];
- }
- array_splice($pinyin,sizeof($pinyin),0,$tmp2);
- }
- }
- }
- else
- {
- foreach ($pinyin as $key => $value)
- {
- $pinyin[$key] .= chr($flow[$i]);
- }
- }
- }
- return $pinyin;
- }
function GetGB2312String($name) - {
- $tostr = "";
- for($i=0;$i {
- $curbin = ord(substr($name,$i,1));
- if($curbin < 0x80)
- {
- $tostr .= substr($name,$i,1);
- }elseif($curbin < bindec("11000000")){
- $str = substr($name,$i,1);
- $tostr .= "".ord($str).";";
- }elseif($curbin < bindec("11100000")){
- $str = substr($name,$i,2);
- $tostr .= "".GetUnicodeChar($str).";";
- $i += 1;
- }elseif($curbin < bindec("11110000")){
- $str = substr($name,$i,3);
- $gstr= iconv("UTF-8","GB2312",$str);
- if(!$gstr)
- {
- $tostr .= "".GetUnicodeChar($str).";";
- }else{
- $tostr .= $gstr;
- }
-
- $i += 2;
- }elseif($curbin < bindec("11111000")){
- $str = substr($name,$i,4);
- $tostr .= "".GetUnicodeChar($str).";";
-
- $i += 3;
- }elseif($curbin < bindec("11111100")){
- $str = substr($name,$i,5);
- $tostr .= "".GetUnicodeChar($str).";";
-
- $i += 4;
- }else{
- $str = substr($name,$i,6);
- $tostr .= "".GetUnicodeChar($str).";";
-
- $i += 5;
- }
- }
-
- return $tostr;
- }
-
- function GetUnicodeChar($str)
- {
- $temp = "";
- for($i=0;$i {
- $x = decbin(ord(substr($str,$i,1)));
- if($i == 0)
- {
- $s = strlen($str)+1;
- $temp .= substr($x,$s,8-$s);
- }else{
- $temp .= substr($x,2,6);
- }
- }
-
- return bindec($temp);
- }
$db = mysql_connect("127.0.0.1:3307", "root","123"); mysql_select_db("qq",$db); - $result = mysql_query("set names utf8",$db);
- $result = mysql_query("select title,group_id from groups",$db);
while ($myrow = mysql_fetch_array($result)) { - $text1="$myrow[0]";
- $text= GetGB2312String($text1);//获得GB2312编码串
- $flow = get_pinyin_array($text);//获得拼音
- $result2 = mysql_query("update groups set titlepinyin='$flow[0]' where group_id = $myrow[1]",$db);
- printf("%s
%s ",$text1,$flow[0]); - }
- ?>
表中加入一个字段titlepinyin
- mysql>alter table groups add titlepinyin varchar(1000);
rpc.php改为
$db = mysql_connect("127.0.0.1:3307", "root","123"); - if(!$db) {
- // Show error if we cannot connect.
- echo 'ERROR: Could not connect to the database.';
- } else {
- // Is there a posted query string?
- if(isset($_POST['queryString'])) {
- $queryString = $_POST['queryString'];
- $length=strlen($queryString);
mysql_select_db("qq",$db); - $result = mysql_query("set names utf8");
// Is the string length greater than 0? - if($length>0) { (程序员之家 bbs.it-home.org 编辑整理)
- $sql="";
- if(!ereg("^[A-Za-z0-9_.-]+$",$queryString))//如果有汉字的话
- {$sql="SELECT title FROM groups WHERE title LIKE '$queryString%' LIMIT 10";
- }else{//英文数字符号
- $sql="SELECT title FROM groups WHERE titlepinyin LIKE '$queryString%' LIMIT 10";
- }
- $query = mysql_query($sql);
- // Run the query: We use LIKE ‘$queryString%’
- if($query) {
- while ($myrow = mysql_fetch_array($query)) {
- // Format the results, im using
- for the list, you can change it.
- // The onClick function fills the textbox with the result.
- echo '
- '.$myrow[0].'
'; - }
- } else {
- echo "ERROR: There was a problem with the query $sql.";
- }
- } else {
- }
- } else {
- echo 'There should be no direct access to this script!';
- }
- }
- ?>
|