MYSQL中的一个问题 数据库中字段title这一列:羽毛球YMQ橄榄球GLQ曲棍球QGQ改成:羽 毛 球 YMQ橄 榄 球 GLQ曲 棍 球 QGQ应该怎么写呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 是在数据库里改,还在php里改? 我想是不是能用正则来匹配这个字段的内容,从左开始匹配,遇到中文就加个空格,遇到数字,英文或者符号,就跳到下个,一直跳到下一个中文,这样一直匹配下去就比如这个意思:羽毛Y-01球YMQ处理后:羽 毛 Y-01 球 YMQ 在PHP代码里改好像很麻烦,要匹配很多。那在mysql数据库中改应该怎么改啊?先这样做看看 <?php$str='羽毛Y-01球YMQ';echo preg_replace('/([\x{4e00}-\x{9fa5}])/u','$1 ',$str);//羽 毛 Y-01球 YMQ用mysql 真心不好处理 用PHP的话把字段内容取出来取出来selsect肯定是个循环,因为有多个数据正则匹配处理preg_replace,将空格加好,再把数据内容更新插入回去update再在的数据库才被改变 $a='羽毛球YMQ';$b=preg_replace('/[A-Za-z](.*)/i','',$a);$b1=str_split($b,2);$b2=implode(" ", $b1);$c=preg_replace("/".$b."/",'',$a);//echo $c;$result=$b2." ".$c;echo $result; <?php$DBServer = "localhost";$DBName = "new";$DBUser = "root";$DBPassword = "";$con = mysql_connect("localhost","root","");mysql_select_db("new");$query = mysql_query("select old_title from pagecontents where old_id = 1 ") or die(mysql_error() );mysql_select_db("new",$con);while ($rows=mysql_fetch_array($query)){ $a = $rows['old_title']; print_r($a); $b = preg_replace('/[A-Za-z](.*)/i','',$a); $b1=str_split($b,2); $b2=implode(" ", $b1); $c=preg_replace("/".$b."/",'',$a); $result=$b2." ".$c; echo $result; mysql_query("set names 'gbk'"); $sql = "update pagecontents set si_title = ('{$result}') where old_id = 1 "; mysql_query($sql) or die(mysql_error()); }?>出来的结果是?????? ??奇怪啊,怎么不是中文啊。 GBK 编码的?mysql_select_db("new");mysql_query("set names 'gbk'");//只要这一句$a='羽毛球YMQ';echo preg_replace('/[\x80-\xff]./', '$0 ', $a); 那一列字段的结构是:utf8_general_ci编码的。存到新的字段的时候,后面设置了set names gbk现在还是?? ?? ????郁闷啊代码还是这样的:<?php$DBServer = "localhost";$DBName = "new";$DBUser = "root";$DBPassword = "";$con = mysql_connect("localhost","root","");mysql_select_db("new");$query = mysql_query("select old_title from pagecontents where old_id = 1 ") or die(mysql_error() );mysql_select_db("new",$con);while ($rows=mysql_fetch_array($query)){ $a = $rows['old_title']; print_r($a); $b = preg_replace('/[A-Za-z](.*)/i','',$a); $b1=str_split($b,2); $b2=implode(" ", $b1); $c=preg_replace("/".$b."/",'',$a); $result=$b2." ".$c; echo $result; mysql_query("set names 'gbk'"); $sql = "update pagecontents set si_title = ('{$result}') where old_id = 1 "; mysql_query($sql) or die(mysql_error()); }?>结果:?? ?? ???? header('Content-Type: text/html; charset=utf-8');$a='羽毛球YMQ';$b=preg_replace('/[A-Za-z](.*)/i','',$a);$b1=str_split($b,3);$b2=implode(" ", $b1);$c=preg_replace("/".$b."/",'',$a);//echo $c;$result=$b2." ".$c;echo $result; 第一个是gbk的 第二个是utf-8的 <?php$DBServer = "localhost";$DBName = "new";$DBUser = "root";$DBPassword = "";$con = mysql_connect("localhost","root","");mysql_query("set names 'gbk'");mysql_select_db("new");$query = mysql_query("select old_title from pagecontents where old_id = 1 ") or die(mysql_error() );mysql_select_db("new",$con);while ($rows=mysql_fetch_array($query)){ $a = $rows['old_title']; print_r($a); $b = preg_replace('/[A-Za-z](.*)/i','',$a); $b1=str_split($b,2); $b2=implode(" ", $b1); $c=preg_replace("/".$b."/",'',$a); $result=$b2." "; echo $result; mysql_query("set names 'gbk'"); $sql = "update pagecontents set si_title = ('{$result}') where old_id = 1 "; mysql_query($sql) or die(mysql_error()); }?>这样就OK了,谢谢大家帮忙。如果中文之间有符号怎么处理呢?例如:羽毛(Y/Z-0.1)球YMQ结果:羽 毛 (Y/Z-0.1) 球 YMQ该怎么改正则呢? <?php$DBServer = "localhost";$DBName = "new";$DBUser = "root";$DBPassword = "";$con = mysql_connect("localhost","root","");mysql_query("set names 'gbk'");mysql_select_db("new");$query = mysql_query("select old_title from pagecontents where old_id = 1 ") or die(mysql_error() );mysql_select_db("new",$con);while ($rows=mysql_fetch_array($query)){ $a = $rows['old_title']; print_r($a); $b = preg_replace('/[A-Za-z](.*)/i','',$a); $b1=str_split($b,2); $b2=implode(" ", $b1); $c=preg_replace("/".$b."/",'',$a); $result=$b2." "; echo $result; mysql_query("set names 'gbk'"); $sql = "update pagecontents set si_title = ('{$result}') where old_id = 1 "; mysql_query($sql) or die(mysql_error()); }?>这段代码处理中文和标点是可以的,但是如果old_title中有英文,或者2个中文汉字之间的数字是单数,就会有bug,英文就不处理后面,2个中文汉字之间的数字是单数就出现乱码,双数则没有问题。求大牛帮忙了。感激不尽!!! 就是遇到这种情况就处理不了:声学a声学建筑和建筑构件隔声测量第6部分(2005年3月21号):楼板撞击声隔声的实验室测量/T19889.6-2005只处理到结果: 声 学--------------------------------------------------------------声学声学建筑和建筑构件隔声测量第6部分(2005年3月21号):楼板撞击声隔声的实验室测量/T19889.6-2005这样就到"6"这里出现乱码:声 学 声 学 建 筑 和 建 筑 构 件 隔 声 测 量 第 6?糠 郑 郝 グ 遄 不 魃 ? 羯 ? 氖 笛 槭 也 饬 html,css。这个问题,也太为难php了 那要不我就不存中文了,转成utf-8码吧? 求php详细的一个开源代码 求救:基于TP做的考试系统随机抽题按照每5题做一页的问题 我的PHPBB3安装最后一步,提示权限错误,可我用的Root用户啊! setcookie 请教各位高手,对这个网有什么修改建议 这是怎么回事?? php 获取到图片的 EXIF 信息 GPSAltitude这个高度是指? 画图? 帝国cms自定义页面上传图片功能如何修改 thinkphp 视频上传 网络错误413 怎么写一个在线下载文件到网站目录 PHP数据库没内容,求解,超级菜鸟。。哎
那在mysql数据库中改应该怎么改啊?先这样做看看
$str='羽毛Y-01球YMQ';
echo preg_replace('/([\x{4e00}-\x{9fa5}])/u','$1 ',$str);
//羽 毛 Y-01球 YMQ用mysql 真心不好处理
肯定是个循环,因为有多个数据
正则匹配处理preg_replace,将空格加好,
再把数据内容更新插入回去update
再在的数据库才被改变
$a='羽毛球YMQ';$b=preg_replace('/[A-Za-z](.*)/i','',$a);
$b1=str_split($b,2);$b2=implode(" ", $b1);
$c=preg_replace("/".$b."/",'',$a);//echo $c;$result=$b2." ".$c;echo $result;
$DBName = "new";
$DBUser = "root";
$DBPassword = "";$con = mysql_connect("localhost","root","");mysql_select_db("new");$query = mysql_query("select old_title from pagecontents where old_id = 1 ") or die(mysql_error() );mysql_select_db("new",$con);while ($rows=mysql_fetch_array($query))
{
$a = $rows['old_title'];
print_r($a);
$b = preg_replace('/[A-Za-z](.*)/i','',$a);
$b1=str_split($b,2);
$b2=implode(" ", $b1);
$c=preg_replace("/".$b."/",'',$a);
$result=$b2." ".$c;
echo $result;
mysql_query("set names 'gbk'");
$sql = "update pagecontents set si_title = ('{$result}') where old_id = 1 ";
mysql_query($sql) or die(mysql_error());
}?>
出来的结果是
?????? ??奇怪啊,怎么不是中文啊。
mysql_query("set names 'gbk'");//只要这一句$a='羽毛球YMQ';
echo preg_replace('/[\x80-\xff]./', '$0 ', $a);
存到新的字段的时候,后面设置了set names gbk
现在还是
?? ?? ????
郁闷啊
代码还是这样的:<?php$DBServer = "localhost";
$DBName = "new";
$DBUser = "root";
$DBPassword = "";$con = mysql_connect("localhost","root","");mysql_select_db("new");$query = mysql_query("select old_title from pagecontents where old_id = 1 ") or die(mysql_error() );mysql_select_db("new",$con);while ($rows=mysql_fetch_array($query))
{
$a = $rows['old_title'];
print_r($a);
$b = preg_replace('/[A-Za-z](.*)/i','',$a);
$b1=str_split($b,2);
$b2=implode(" ", $b1);
$c=preg_replace("/".$b."/",'',$a);
$result=$b2." ".$c;
echo $result;
mysql_query("set names 'gbk'");
$sql = "update pagecontents set si_title = ('{$result}') where old_id = 1 ";
mysql_query($sql) or die(mysql_error());
}?>结果:
?? ?? ????
header('Content-Type: text/html; charset=utf-8');
$a='羽毛球YMQ';$b=preg_replace('/[A-Za-z](.*)/i','',$a);
$b1=str_split($b,3);$b2=implode(" ", $b1);
$c=preg_replace("/".$b."/",'',$a);//echo $c;$result=$b2." ".$c;echo $result;
<?php$DBServer = "localhost";
$DBName = "new";
$DBUser = "root";
$DBPassword = "";$con = mysql_connect("localhost","root","");mysql_query("set names 'gbk'");
mysql_select_db("new");$query = mysql_query("select old_title from pagecontents where old_id = 1 ") or die(mysql_error() );mysql_select_db("new",$con);while ($rows=mysql_fetch_array($query))
{
$a = $rows['old_title'];
print_r($a);
$b = preg_replace('/[A-Za-z](.*)/i','',$a);
$b1=str_split($b,2);
$b2=implode(" ", $b1);
$c=preg_replace("/".$b."/",'',$a);
$result=$b2." ";
echo $result;
mysql_query("set names 'gbk'");
$sql = "update pagecontents set si_title = ('{$result}') where old_id = 1 ";
mysql_query($sql) or die(mysql_error());
}?>
这样就OK了,谢谢大家帮忙。如果中文之间有符号怎么处理呢?
例如:
羽毛(Y/Z-0.1)球YMQ结果:
羽 毛 (Y/Z-0.1) 球 YMQ该怎么改正则呢?
$DBName = "new";
$DBUser = "root";
$DBPassword = "";$con = mysql_connect("localhost","root","");mysql_query("set names 'gbk'");
mysql_select_db("new");$query = mysql_query("select old_title from pagecontents where old_id = 1 ") or die(mysql_error() );mysql_select_db("new",$con);while ($rows=mysql_fetch_array($query))
{
$a = $rows['old_title'];
print_r($a);
$b = preg_replace('/[A-Za-z](.*)/i','',$a);
$b1=str_split($b,2);
$b2=implode(" ", $b1);
$c=preg_replace("/".$b."/",'',$a);
$result=$b2." ";
echo $result;
mysql_query("set names 'gbk'");
$sql = "update pagecontents set si_title = ('{$result}') where old_id = 1 ";
mysql_query($sql) or die(mysql_error());
}?>这段代码处理中文和标点是可以的,但是如果old_title中有英文,或者2个中文汉字之间的数字是单数,就会有bug,英文就不处理后面,2个中文汉字之间的数字是单数就出现乱码,双数则没有问题。求大牛帮忙了。感激不尽!!!