我要在查询数据库之后截取得到的字符串,如果大于10个字符截取前10个后面的用...代替
                                     等于和小于10个字符直接显示
数据库中的值有汉字,英文,数字,汉字和英文混排
这个截取函数怎么写(最好不在页面写,并且希望把你的代码贴出来)

解决方案 »

  1.   


    //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;
    }
    ?>
      

  2.   


    $str = '@#¥@%¥#%';
    $charset = 'GBK';//UTF-8$str = mb_strlen($str, $charset)<=10 ? $str : mb_substr($str, 0, 10, $charset) . '...';
      

  3.   


    输出的结果是乱码   
    id number title 
    2    权权   漯td>  
    3    444   漯td>  
    4    333   漯td>  
    5    888   漯td> 
      

  4.   


    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/>";
      

  5.   


    确认你使用的编码是GBK了么??
      

  6.   

    ord(substr(str,start,len))>0xaa
    $str.=$str.substring($str,$i,2);....
    截取中文 网上有很多的源码 可以谷歌
      

  7.   

    我用的是utf-8   我改了
      

  8.   

    你说的很对   我在网上找了一些   结合我改了一下
    大家帮我看看那错了
    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;
    }
    }
      

  9.   

      PHP code

    $this->cutString($list[0]['title'],10);
    $this->assign('data',$$data);
      

  10.   

    哈哈。ecshop里面的很强大。我很喜欢。
    楼主可以去看看
      

  11.   

    PHP code<?php
    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中)
      

  12.   

     我用你的这个
    $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/>";这两个没看明白  你是第一个要引用第二个啊  还是怎么回事
      

  13.   

    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);
    }
    $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);
    //这样明白了吧
      

  14.   

    出现乱码  编码问题数据读出是utf-8吗
      

  15.   


    编码是utf-8的他是在存入data输出才是乱码  直接输出不是
      

  16.   

    是的  其实也不应该说他是乱码 只不过数据库中没有的
    权$list杮.....
    测试标鮮....
    像上面的"柿"和"鲜"是数据库中没有的
      

  17.   

    查询出的数据打印出来看看
    你使用了哪个函数utf_substrcutstr
      

  18.   

    怀疑楼主的数据库中数据是乱码,要不上面那么多正确答案,早搞定了....楼主用length和str_length检查你数据库中汉字长度(是mysql里)
      

  19.   

    回答25楼      我用的是这个substr函数回答26楼      数据库中没有乱码
      

  20.   

    UTf-8我现在感觉应该不是编码的问题   你看啊 
    我截取前6个字符一个汉字是3占三个六个正好是2个汉字也就是说
     截取的那个数可以整除3的话就没有乱码否则就有
      

  21.   

    谈下思路:
    主要搞明白utf8下,汉字、数字、英文字母、特殊符号各占几个字节。
    对待处理串逐个字符判断类型,加字节长度,直到长度到达最大长度
    具体判断和处理,上面都提到了,你也可再搜搜。
      

  22.   

    function cut_str($string, $sublen, $start = 0, $code = 'utf-8')
    {
        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;
        }
    }