function cutstr($string,$length,$havedot=0)
{
if(strlen($string)<=$length){
return $string;
}
$wordscut = mb_substr($string,0,$length,"utf-8");
if($havedot){
return $wordscut.'...';
}else {
return $wordscut.'&nbsp;';
}
}调用方法:
$s = "sdfsdfdsfdsfdsfs士大夫上的,士大夫@#";
echo cutstr($s,20,$havedot=1);当大于20的时候,进行切割,默认切割后的后缀是"..."

解决方案 »

  1.   


    public function mySubstr ( $str='',$length='',$encoding='UTF-8')
        {
            if( strlen($str) > $length ){
                $str =mb_substr($str,0,ceil($length/3)-1,$encoding).'...'; 
            } 
            return $str;
        }也不知道你的是什么问题,我只有把我简单写的给你了哦...
      

  2.   

    $str = mb_strimwidth($str, 0, 20, "...");
      

  3.   


    $s = "sdfsdfdsfdsfdsfs士大夫上的,士大夫@#";
    print_r(mb_strimwidth($s, 0, 20, '...'););mb_strimwidth
    (PHP 4 >= 4.0.6, PHP 5)mb_strimwidth — Get truncated string with specified width说明
    string mb_strimwidth ( string $str , int $start , int $width [, string $trimer [, string $encoding ]] )
    Truncates string str to specified width. 参数str 
    The string being decoded. start 
    The start position offset. Number of characters from the beginning of string. (First character is 0) width 
    The width of the desired trim. trimer 
    A string that is added to the end of string when string is truncated. encoding 
    encoding 参数为字符编码。如果省略,则使用内部字符编码。
    返回值
    The truncated string. If trimer is set, trimer is appended to the return value. 范例Example #1 mb_strimwidth() example<?php
    echo mb_strimwidth("Hello World", 0, 10, "...");
    // outputs Hello W...
    ?> 
      

  4.   

    mb_strimwidth对于中文情况会比较麻烦,mb_strimwidth会把中文字符看作字节来判断,如果要切的位置刚好是个中文,就会变成乱码了...所以对待中文的情况还是得自己写个function,如果没有英文的情况,还是用mb_strimwidth来的方便。
      

  5.   

    用这个mb_substr,什么中英文都可以了。$string="sdfsdfds123士大夫上的¥33";
    echo mb_substr($string,0,10,"utf-8");
    其实这个一个函数就可以搞定了,上面写了一个函数是为了判断在切割后加一个“...”,让别人知道这是被切割了的
      

  6.   

    我这里也有一种思路,是按照字数来取,蛮适合字数比较少的时候function substring($str,$start=0;$length=200,$suffix="...")
    {
          preg_match_all("/(\S)/u",$str,$res);
          return join("",array_slice($res[1],$start,$length)).$suffix;

      

  7.   

    不好意思,打错了一个字符function substring($str,$start=0,$length=200,$suffix="...")
    {
          preg_match_all("/(\S)/u",$str,$res);
          return join("",array_slice($res[1],$start,$length)).$suffix;

      

  8.   


    mb_strimwidth乱码解决方法今天和大家讨论下mb_strimwidth乱码解决方法,个人设计的wordpress主题都是用这个方式来实现文字截断,但是有些服务器会出现乱码,其原因是服务器没有打开mb_string extension服务, 解决的方法是用sub_str来实现文字截断,大概介绍下这两种方法。1. mb_strimwidth文字截断需要服务器打开mb_string extension<?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 200,""); ?>
    2. cut_str文字截断需要将一下代码放加入到function.php 文件function cut_str($sourcestr,$cutlength)
    {
    $returnstr='';
    $i=0;
    $n=0;
    $str_length=strlen($sourcestr);//字符串的字节数
    while (($n<$cutlength) and ($i<=$str_length))
    {
    $temp_str=substr($sourcestr,$i,1);
    $ascnum=Ord($temp_str);//得到字符串中第$i位字符的ascii码
    if ($ascnum>=224)    //如果ASCII位高与224,
    {
    $returnstr=$returnstr.substr($sourcestr,$i,3); //根据UTF-8编码规范,将3个连续的字符计为单个字符
    $i=$i+3;            //实际Byte计为3
    $n++;            //字串长度计1
    }
    elseif ($ascnum>=192) //如果ASCII位高与192,
    {
    $returnstr=$returnstr.substr($sourcestr,$i,2); //根据UTF-8编码规范,将2个连续的字符计为单个字符
    $i=$i+2;            //实际Byte计为2
    $n++;            //字串长度计1
    }
    elseif ($ascnum>=65 && $ascnum<=90) //如果是大写字母,
    {
    $returnstr=$returnstr.substr($sourcestr,$i,1);
    $i=$i+1;            //实际的Byte数仍计1个
    $n++;            //但考虑整体美观,大写字母计成一个高位字符
    }
    else                //其他情况下,包括小写字母和半角标点符号,
    {
    $returnstr=$returnstr.substr($sourcestr,$i,1);
    $i=$i+1;            //实际的Byte数计1个
    $n=$n+0.5;        //小写字母和半角标点等与半个高位字符宽...
    }
    }
    if ($str_length>$cutlength){
    $returnstr = $returnstr . "...";//超过长度时在尾处加上省略号
    }
    return $returnstr;
    }
    使用方法如下:将下面代码放到需要的地方<?php echo cut_str(strip_tags(apply_filters('the_content', $post->post_content)),200,""); ?>
      

  9.   


    /******************************************************************
    * PHP截取UTF-8字符串,解决半字符问题。
    * 英文、数字(半角)为1字节(8位),中文(全角)为3字节
    * @return 取出的字符串, 当$len小于等于0时, 会返回整个字符串
    * @param $str 源字符串
    * $len 左边的子串的长度
    ****************************************************************/
    function utf_substr($str,$len,$endString="..."){
    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).$endString;
    }
    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]);
    }
      

  10.   

    //字符串截取(适用于中英文字符串)截取后用代替
    function cutString($string='',$length=20,$endString='...')
    {
        $string = trim($string);
        if('' == $string) {
            return '';
        }    $oldLength = strlen($string);
        $newString= array();
        for($i=0;$i<$length;$i++) {
            $tmpString = substr($string, 0, 1);
            if(ord($tmpString)>127) {
                $i++;
                if($i<$length) {
                    $newString[] = substr($string, 0, 3);
                    $string = substr($string,3);
                }
            }
            else {
                $newString[] = substr($string, 0, 1);
                $string = substr($string, 1);
            }
        }    $newString = implode('',$newString);    return strlen($newString) < $oldLength ? $newString.$endString : $newString;
    }