function FSubstr($title,$start,$len="",$magic=true)
{
/**
   *   powered by Smartpig
   *  mailto:[email protected]
   */
  
$length = 0;
if($len == "") $len = strlen($title);
//判断起始为不正确位置
if($start > 0)
{
   $cnum = 0;
   for($i=0;$i<$start;$i++)
   {
    if(ord(substr($title,$i,1)) >= 128) $cnum ++;
   }
   if($cnum%2 != 0) $start--;
  
   unset($cnum);
}
if(strlen($title)<=$len) return substr($title,$start,$len);
$alen    = 0;
$blen = 0;
$realnum = 0;
for($i=$start;$i<strlen($title);$i++)
{
   $ctype = 0;
   $cstep = 0;
   $cur = substr($title,$i,1);
   if($cur == "&")
   {
    if(substr($title,$i,4) == "<")
    {
     $cstep = 4;
     $length += 4;
     $i += 3;
     $realnum ++;
     if($magic)
     {
      $alen ++;
     }
    }
    else if(substr($title,$i,4) == ">")
    {
     $cstep = 4;
     $length += 4;
     $i += 3;
     $realnum ++;
     if($magic)
     {
      $alen ++;
     }
    }
    else if(substr($title,$i,5) == "&")
    {
     $cstep = 5;
     $length += 5;
     $i += 4;
     $realnum ++;
     if($magic)
     {
      $alen ++;
     }
    }
    else if(substr($title,$i,6) == """)
    {
     $cstep = 6;
     $length += 6;
     $i += 5;
     $realnum ++;
     if($magic)
     {
      $alen ++;
     }
    }
    else if(substr($title,$i,6) == "'")
    {
     $cstep = 6;
     $length += 6;
     $i += 5;
     $realnum ++;
     if($magic)
     {
      $alen ++;
     }
    }
    else if(preg_match("/&#(\d+);/i",substr($title,$i,8),$match))
    {
     $cstep = strlen($match[0]);
     $length += strlen($match[0]);
     $i += strlen($match[0])-1;
     $realnum ++;
     if($magic)
     {
      $blen ++;
      $ctype = 1;
     }
    }
   }else{
    if(ord($cur)>=128)
    {
     $cstep = 2;
     $length += 2;
     $i += 1;
     $realnum ++;
     if($magic)
     {
      $blen ++;
      $ctype = 1;
     }
    }else{
     $cstep = 1;
     $length +=1;
     $realnum ++;
     if($magic)
     {
      $alen++;
     }
    }
   }
  
   if($magic)
   {
    if(($blen*2+$alen) == ($len*2)) break;
    if(($blen*2+$alen) == ($len*2+1))
    {
     if($ctype == 1)
     {
      $length -= $cstep;
      break;
     }else{
      break;
     }
    }
   }else{
    if($realnum == $len) break;
   }
}
unset($cur);
unset($alen);
unset($blen);
unset($realnum);
unset($ctype);
unset($cstep);
return substr($title,$start,$length);
}
这个截取字符串的 ,我想在截取后字符串添加个 ...省略号   高手帮忙修改一下

解决方案 »

  1.   

    return substr($title,$start,$length).'...'; 
      

  2.   

    要是  字符串 与 $len     相等的话    也会出现..   我想让相等的时候不出现 ...
      

  3.   

    那就
    if(strlen($str)==$len){
    return substr($title,$start,$length);
    }else{
    return substr($title,$start,$length).'...'; 
    }
      

  4.   

    $str  这个怎么得来啊 
      

  5.   

    给个好用的你啦
    <?php
    /*
    Utf-8、gb2312都支持的汉字截取函数
    cut_str(字符串, 截取长度, 开始长度, 编码);
    编码默认为 utf-8
    开始长度默认为 0
    */
    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;
        }
    }
    ?>
      

  6.   


    我来替他回答你,$str就是你要判断的字符串,谢谢
      

  7.   


    $str = mb_strimwidth($str, 0, 40, "..>");