我要在查询数据库之后截取得到的字符串,如果大于10个字符截取前10个后面的用...代替
等于和小于10个字符直接显示
数据库中的值有汉字,英文,数字,汉字和英文混排
这个截取函数怎么写(最好不在页面写,并且希望把你的代码贴出来)
等于和小于10个字符直接显示
数据库中的值有汉字,英文,数字,汉字和英文混排
这个截取函数怎么写(最好不在页面写,并且希望把你的代码贴出来)
解决方案 »
- FULLTEXT 如何匹配符号
- UNION ALL链接多个表查询,怎么知道数据来自哪个表?
- 想问下自定义函数能取代类的作用不?
- php如何刷新某个网页?
- 求高手将这段JS算法用PHP来实现.
- 怎么在电脑里面同时安装商城的程序和另外一个discuz的程序?
- 关于正则表达式的问题("(\{)(\r\n)*(.*)(\})"),精通者请进
- ecshop这个url重写哪里出错了
- phpstorm中出现502bad gateway错误怎么办?路径什么的都配置好了但是没用,求大神解救
- 为什么php输出是空白的?
- PHP实现全选,删除(更新某一列的值)或者是审核(更新某一列的值) 100分
- apache+php错误
//PHPWind的中文截取函数:function substrs($content,$length,$add='Y'){
if ($length && strlen($content)>$length) {
global $db_charset;
if ($db_charset!='utf-8') {
$retstr = '';
for ($i=0;$i<$length-2;$i++) {
$retstr .= ord($content[$i]) > 127 ? $content[$i].$content[++$i] : $content[$i];
}
return $retstr.($add=='Y' ? ' ..' : '');
}
return utf8_trim(substr($content,0,$length)).($add=='Y' ? ' ..' : '');
}
return $content;
}function utf8_trim($str) {
$hex = ”;
$len = strlen($str)-1;
for ($i=$len;$i>=0;$i-=1) {
$ch = ord($str[$i]);
$hex .= ” $ch”;
if (($ch & 128)==0 || ($ch & 192)==192) {
return substr($str,0,$i);
}
}
return $str.$hex;
}
?>//Discuz的截取函数:function cutstr($string, $length, $dot = ‘ …’) {
global $charset;if(strlen($string) <= $length) {
return $string;
}$string = str_replace(array(‘&’, ‘”‘, ‘<’, ‘>’), array(‘&’, ‘”‘, ‘<’, ‘>’), $string);$strcut = ”;
if(strtolower($charset) == ‘utf-8′) {$n = $tn = $noc = 0;
while($n < strlen($string)) {$t = ord($string[$n]);
if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
$tn = 1; $n++; $noc++;
} elseif(194 <= $t && $t <= 223) {
$tn = 2; $n += 2; $noc += 2;
} elseif(224 <= $t && $t < 239) {
$tn = 3; $n += 3; $noc += 2;
} elseif(240 <= $t && $t <= 247) {
$tn = 4; $n += 4; $noc += 2;
} elseif(248 <= $t && $t <= 251) {
$tn = 5; $n += 5; $noc += 2;
} elseif($t == 252 || $t == 253) {
$tn = 6; $n += 6; $noc += 2;
} else {
$n++;
}if($noc >= $length) {
break;
}}
if($noc > $length) {
$n -= $tn;
}$strcut = substr($string, 0, $n);} else {
for($i = 0; $i < $length; $i++) {
$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
}
}$strcut = str_replace(array(‘&’, ‘”‘, ‘<’, ‘>’), array(‘&’, ‘”‘, ‘<’, ‘>’), $strcut);return $strcut.$dot;
}
?>
$str = '@#¥@%¥#%';
$charset = 'GBK';//UTF-8$str = mb_strlen($str, $charset)<=10 ? $str : mb_substr($str, 0, 10, $charset) . '...';
输出的结果是乱码
id number title
2 权权 漯td>
3 444 漯td>
4 333 漯td>
5 888 漯td>
header("Content-type:text/html;charset=utf-8");
$lenth = 10; //指定字节
$str = "aa怎么将新闻的截断cmkcmkscmakcmcmmckscmsk"; //要截断的数据,1中文等于2字节
//echo strlen($str);
//echo strlen($str)<=$lenth ? $str : (substr($str,0,$lenth)."...."); /******************************************************************
* PHP截取UTF-8字符串,解决半字符问题。
* 英文、数字(半角)为1字节(8位),中文(全角)为3字节
* @return 取出的字符串, 当$len小于等于0时, 会返回整个字符串
* @param $str 源字符串
* $len 左边的子串的长度
****************************************************************/
function utf_substr($str,$len){
for($i=0;$i<$len;$i++){
$temp_str=substr($str,0,1);
if(ord($temp_str) > 127){
$i++;
if($i<$len){
$new_str[]=substr($str,0,3);
$str=substr($str,3);
}
}else{
$new_str[]=substr($str,0,1);
$str=substr($str,1);
}
}
return join($new_str);
}
function cutstr($string, $length) {
preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $info);
for($i=0; $i<count($info[0]); $i++) {
$wordscut .= $info[0][$i];
$j = ord($info[0][$i]) > 127 ? $j + 2 : $j + 1;
if ($j > $length - 3) {
return $wordscut."...";
}
}
return join('', $info[0]);
}echo utf_substr($str,$lenth)."<br/>";
echo cutstr($str,$lenth)."<br/>";
确认你使用的编码是GBK了么??
$str.=$str.substring($str,$i,2);....
截取中文 网上有很多的源码 可以谷歌
大家帮我看看那错了
function cutString($string,$beginIndex = 0,$length=15,$endString='...')
{
$string = trim($string);
// echo "$string";
if('' == $string) {
return '';
}
$oldLength = strlen($string);
$newString= array();
for($i=0;$i<$length;$i++) {
$tmpString = substr($string, 0, 10);
if(ord($tmpString)>127) {
$i++;
if($i<$length) {
$newString[] = substr($string, 0, 3);
$string = substr($string,3);
}
}
else {
$newString[] = substr($string, 0, 3);
$string = substr($string,1);
}
echo ord($tmpString);
}
$newString = implode('',$newString);
// return strlen($newString) < $oldLength ? $newString.$endString : $newString;
if ( strlen($newString) < $oldLength) {
echo $newString.$endString;exit();
$data = $newString.$endString;
}else {
echo $newString;exit();
$data = $newString;
}
}
$this->cutString($list[0]['title'],10);
$this->assign('data',$$data);
楼主可以去看看
function getAll(){
$business = D('business');
$this->count();
$list=$business->select();
$this->cutString($list[0]['title'],10);
$this->assign('list',$list);
$this->display('getAll');
}
function cutString($string,$beginIndex = 0,$length=15,$endString='...')
{
$string = trim($string);
if('' == $string) {
return '';
}
$oldLength = strlen($string);
$newString= array();
for($i=0;$i<$length;$i++) {
$tmpString = substr($string, 0, 10);
if(ord($tmpString)>127) {
$i++;
if($i<$length) {
$newString[] = substr($string, 0, 3);
$string = substr($string,3);
}
}
else {
$newString[] = substr($string, 0, 3);
$string = substr($string,1);
}
}
$newString = implode('',$newString);
// return strlen($newString) < $oldLength ? $newString.$endString : $newString;
if ( strlen($newString) < $oldLength) {
$data = $newString.$endString;
}else {
$data = $newString;
}
$this->assign('data',$data);
}
?>
这个方法可以用但是不知道为什么输出的结果是乱码(存到data中)
$lenth = 10; //指定字节
$str = $list[0][title]; //要截断的数据,1中文等于2字节
echo strlen($str);
echo strlen($str)<=$lenth ? $str : (substr($str,0,$lenth)."...."); exit();
运行有乱码
function utf_substr($str,$len){
for($i=0;$i<$len;$i++){
$temp_str=substr($str,0,1);
if(ord($temp_str) > 127){
$i++;
if($i<$len){
$new_str[]=substr($str,0,3);
$str=substr($str,3);
}
}else{
$new_str[]=substr($str,0,1);
$str=substr($str,1);
}
}
return join($new_str);
}
function cutstr($string, $length) {
preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $info);
for($i=0; $i<count($info[0]); $i++) {
$wordscut .= $info[0][$i];
$j = ord($info[0][$i]) > 127 ? $j + 2 : $j + 1;
if ($j > $length - 3) {
return $wordscut."...";
}
}
return join('', $info[0]);
}echo utf_substr($str,$lenth)."<br/>";
echo cutstr($str,$lenth)."<br/>";这两个没看明白 你是第一个要引用第二个啊 还是怎么回事
for($i=0;$i<$len;$i++){
$temp_str=substr($str,0,1);
if(ord($temp_str) > 127){
$i++;
if($i<$len){
$new_str[]=substr($str,0,3);
$str=substr($str,3);
}
}else{
$new_str[]=substr($str,0,1);
$str=substr($str,1);
}
}
return join($new_str);
}
$lenth = 10;
$str = $list[0][title];
echo utf_substr($str,$lenth);
function cutstr($string, $length) {
preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $info);
for($i=0; $i<count($info[0]); $i++) {
$wordscut .= $info[0][$i];
$j = ord($info[0][$i]) > 127 ? $j + 2 : $j + 1;
if ($j > $length - 3) {
return $wordscut."...";
}
}
return join('', $info[0]);
}
$lenth = 10;
$str = $list[0][title];
echo cutstr($str,$lenth);
//这样明白了吧
编码是utf-8的他是在存入data输出才是乱码 直接输出不是
权$list杮.....
测试标鮮....
像上面的"柿"和"鲜"是数据库中没有的
你使用了哪个函数utf_substrcutstr
我截取前6个字符一个汉字是3占三个六个正好是2个汉字也就是说
截取的那个数可以整除3的话就没有乱码否则就有
主要搞明白utf8下,汉字、数字、英文字母、特殊符号各占几个字节。
对待处理串逐个字符判断类型,加字节长度,直到长度到达最大长度
具体判断和处理,上面都提到了,你也可再搜搜。
{
if($code == 'utf-8')
{
$pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
preg_match_all($pa, $string, $t_string); if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
return join('', array_slice($t_string[0], $start, $sublen));
}
else
{
$start = $start*2;
$sublen = $sublen*2;
$strlen = strlen($string);
$tmpstr = ''; for($i=0; $i< $strlen; $i++)
{
if($i>=$start && $i< ($start+$sublen))
{
if(ord(substr($string, $i, 1))>129)
{
$tmpstr.= substr($string, $i, 2);
}
else
{
$tmpstr.= substr($string, $i, 1);
}
}
if(ord(substr($string, $i, 1))>129) $i++;
}
if(strlen($tmpstr)< $strlen ) $tmpstr.= "...";
return $tmpstr;
}
}