mysql 里实现查询汉字的拼音首字母 哪位大侠 给我个 mysql 里实现查询汉字的拼音首字母 能用的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://topic.csdn.net/u/20090611/17/7579BB9A-0A7C-4460-A302-10BAE7F4A9C5.html 去mysql版块求解,那边大牛很多。 #2 的连接中给出了解决方案,但也明确的说到适用范围很窄(国标一级字库:3755个汉字)所以你应该构造一个更大的对照表$fn = 'pyi.txt';if(! file_exists($fn)) { $url = 'http://blog.csdn.net/xuzuning/article/details/5611291'; $s = file_get_contents($url); preg_match('#CODE<br />(.+)<br />CODE#is', $s, $r); $s = str_replace('&nb'.'sp;', ' ', $r[1]); file_put_contents($fn, $s);}$s = file_get_contents($fn);for($i=0; $i<strlen($s); $i++) { if($s{$i} == ' ') continue; $h = floor($i / 0xbf) + 0x81; $l = ($i % 0xbf) + 0x40; echo chr($h) . chr($l) . $s{$i};}chr($h) . chr($l) 是GBK编码的汉字$s{$i} 是对应的拼音首字母你将他们入库就可以了 大神给句 查询的SQL给我看看 你建立的表是 utf8 的我生成的数据是 gbk 的你只需在数据插入表之前执行一次SQL指令: set names gbk剩下的事情 mysql 会替你完成 function getfirstchar($s0){ if($s0[0]=='I' || $s[0]=='i'){ return "I"; }elseif($s0[0]=='U' || $s[0]=='u'){ return 'U'; }elseif($s0[0]=='V' || $s[0]=='v'){ return 'V'; }else{ $fchar = ord($s0{0}); if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0}); $s1 = iconv("UTF-8","gb2312", $s0); $s2 = iconv("gb2312","UTF-8", $s1); if($s2 == $s0){$s = $s1;}else{$s = $s0;} $asc = ord($s{0}) * 256 + ord($s{1}) - 65536; if($asc >= -20319 and $asc <= -20284) return "A"; if($asc >= -20283 and $asc <= -19776) return "B"; if($asc >= -19775 and $asc <= -19219) return "C"; if($asc >= -19218 and $asc <= -18711) return "D"; if($asc >= -18710 and $asc <= -18527) return "E"; if($asc >= -18526 and $asc <= -18240) return "F"; if($asc >= -18239 and $asc <= -17923) return "G"; if($asc >= -17922 and $asc <= -17418) return "H"; if($asc >= -17417 and $asc <= -16475) return "J"; if($asc >= -16474 and $asc <= -16213) return "K"; if($asc >= -16212 and $asc <= -15641) return "L"; if($asc >= -15640 and $asc <= -15166) return "M"; if($asc >= -15165 and $asc <= -14923) return "N"; if($asc >= -14922 and $asc <= -14915) return "O"; if($asc >= -14914 and $asc <= -14631) return "P"; if($asc >= -14630 and $asc <= -14150) return "Q"; if($asc >= -14149 and $asc <= -14091) return "R"; if($asc >= -14090 and $asc <= -13319) return "S"; if($asc >= -13318 and $asc <= -12839) return "T"; if($asc >= -12838 and $asc <= -12557) return "W"; if($asc >= -12556 and $asc <= -11848) return "X"; if($asc >= -11847 and $asc <= -11056) return "Y"; if($asc >= -11055 and $asc <= -10247) return "Z"; return null; } }这是我之前的做的,希望可以帮到您! 不对,从图片上看,你的 HZ字段都是“涓”这显然是utf-8编码字符在gbk环境中的表现,无端端的弄什么“国际化”!自找麻烦,还浪费50%空间 SQL 语句给我看下行吗? mysql_query('set names gbk');这里是#7的代码,将其中的echo chr($h) . chr($l) . $s{$i};改为$ch = chr($h) . chr($l);$sql = 'insert into 表名 (HZ, ximu) values ('$ch','{$s{$i}}')";mysql_query($sql); 就是按照你这种方案走的效果 还是一样有图有真相1.建表是UTF8的2.插数据3.查看数据还是这样 UT8表 里能插GBK的数据? 你搞太乱了,这明显乱码了,表是utf-8,你就set names utf8呗,往数据库里存要以数据库为准 #18中你的sql语句都生成了,怎么还 set names gbk ??? 我的表是UTF8的么 你的汉字 不是 GBK的么我当然要转换成GBK了 你生成了那些 SQL 指令是保存在文件里的吗?保存时的编码是什么? utf-8 的吧? 我是PHP生成的 <?php$fn = 'pyi.txt';if(! file_exists($fn)) { $url = 'http://blog.csdn.net/xuzuning/article/details/5611291'; $s = file_get_contents($url); preg_match('#CODE<br />(.+)<br />CODE#is', $s, $r); $s = str_replace('&nb'.'sp;', ' ', $r[1]); file_put_contents($fn, $s);}$s = file_get_contents($fn);for($i=0; $i<strlen($s); $i++) { if($s{$i} == ' ') continue; $h = floor($i / 0xbf) + 0x81; $l = ($i % 0xbf) + 0x40;// echo chr($h) . chr($l) . $s{$i};$HZ = chr($h) . chr($l);$zimu = $s{$i}; echo "INSERT INTO `piying5` (`HZ`, `zimu`) VALUES ('$HZ', '$zimu');<br/>";}?>这样生成的 然后复制到那里执行的 set names utf8试一下 现在这个表都把我搞糊涂了 到现在 我都没 搞明白 假如另一个表 有name但是 根据 拼音到底怎么查? php数组的简单问题 求助一下,下面这一段代码有没有办法优化一下性能 js中的一些问题.求些MYSQL的常用命令. 请教作过UC方面的高手!!!!! php asp.net那个开发语言更好! 提交参数的问题!!! php 在 linux 下 调用c文件 本人的论坛由dvbbs8.2升级到phpwind8.7后,不能发视频flash了? 如何显示得到的访问者的ip地址所属的省份和城市? 求大神帮忙,thinkPHP的数据库连接问题 帮忙解决个php正则 谁能帮我看看这个php里面的加密方法是什么
所以你应该构造一个更大的对照表$fn = 'pyi.txt';
if(! file_exists($fn)) {
$url = 'http://blog.csdn.net/xuzuning/article/details/5611291';
$s = file_get_contents($url);
preg_match('#CODE<br />(.+)<br />CODE#is', $s, $r);
$s = str_replace('&nb'.'sp;', ' ', $r[1]);
file_put_contents($fn, $s);
}
$s = file_get_contents($fn);
for($i=0; $i<strlen($s); $i++) {
if($s{$i} == ' ') continue;
$h = floor($i / 0xbf) + 0x81;
$l = ($i % 0xbf) + 0x40;
echo chr($h) . chr($l) . $s{$i};
}chr($h) . chr($l) 是GBK编码的汉字
$s{$i} 是对应的拼音首字母
你将他们入库就可以了
我生成的数据是 gbk 的
你只需在数据插入表之前执行一次SQL指令: set names gbk
剩下的事情 mysql 会替你完成
if($s0[0]=='I' || $s[0]=='i'){
return "I";
}elseif($s0[0]=='U' || $s[0]=='u'){
return 'U';
}elseif($s0[0]=='V' || $s[0]=='v'){
return 'V';
}else{
$fchar = ord($s0{0});
if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0});
$s1 = iconv("UTF-8","gb2312", $s0);
$s2 = iconv("gb2312","UTF-8", $s1);
if($s2 == $s0){$s = $s1;}else{$s = $s0;}
$asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
if($asc >= -20319 and $asc <= -20284) return "A";
if($asc >= -20283 and $asc <= -19776) return "B";
if($asc >= -19775 and $asc <= -19219) return "C";
if($asc >= -19218 and $asc <= -18711) return "D";
if($asc >= -18710 and $asc <= -18527) return "E";
if($asc >= -18526 and $asc <= -18240) return "F";
if($asc >= -18239 and $asc <= -17923) return "G";
if($asc >= -17922 and $asc <= -17418) return "H";
if($asc >= -17417 and $asc <= -16475) return "J";
if($asc >= -16474 and $asc <= -16213) return "K";
if($asc >= -16212 and $asc <= -15641) return "L";
if($asc >= -15640 and $asc <= -15166) return "M";
if($asc >= -15165 and $asc <= -14923) return "N";
if($asc >= -14922 and $asc <= -14915) return "O";
if($asc >= -14914 and $asc <= -14631) return "P";
if($asc >= -14630 and $asc <= -14150) return "Q";
if($asc >= -14149 and $asc <= -14091) return "R";
if($asc >= -14090 and $asc <= -13319) return "S";
if($asc >= -13318 and $asc <= -12839) return "T";
if($asc >= -12838 and $asc <= -12557) return "W";
if($asc >= -12556 and $asc <= -11848) return "X";
if($asc >= -11847 and $asc <= -11056) return "Y";
if($asc >= -11055 and $asc <= -10247) return "Z";
return null;
}
}
这是我之前的做的,希望可以帮到您!
这显然是utf-8编码字符在gbk环境中的表现,无端端的弄什么“国际化”!自找麻烦,还浪费50%空间
SQL 语句给我看下行吗?
echo chr($h) . chr($l) . $s{$i};
改为
$ch = chr($h) . chr($l);
$sql = 'insert into 表名 (HZ, ximu) values ('$ch','{$s{$i}}')";
mysql_query($sql);
就是按照你这种方案走的效果 还是一样
有图有真相1.建表是UTF8的
2.插数据3.查看数据
还是这样 UT8表 里能插GBK的数据?
你的汉字 不是 GBK的么
我当然要转换成GBK了
保存时的编码是什么? utf-8 的吧?
<?php
$fn = 'pyi.txt';
if(! file_exists($fn)) {
$url = 'http://blog.csdn.net/xuzuning/article/details/5611291';
$s = file_get_contents($url);
preg_match('#CODE<br />(.+)<br />CODE#is', $s, $r);
$s = str_replace('&nb'.'sp;', ' ', $r[1]);
file_put_contents($fn, $s);
}
$s = file_get_contents($fn);
for($i=0; $i<strlen($s); $i++) {
if($s{$i} == ' ') continue;
$h = floor($i / 0xbf) + 0x81;
$l = ($i % 0xbf) + 0x40;
// echo chr($h) . chr($l) . $s{$i};
$HZ = chr($h) . chr($l);
$zimu = $s{$i};
echo "INSERT INTO `piying5` (`HZ`, `zimu`) VALUES ('$HZ', '$zimu');<br/>";
}?>这样生成的 然后复制到那里执行的
试一下
到现在 我都没 搞明白 假如另一个表 有name但是 根据 拼音到底怎么查?