function csubstr( $str, $start, $len, $charset )
{
  if ( $charset == "utf-8" )
  {
    $tmpstr = utf8_substr( $str, $start, $len );
    
    return $tmpstr;
  }
  else
  {
    $len = $len - 1;
    $strlen = strlen( $str );
    if ( $strlen <= $start )
    {
      return $str;
    }
    $clen = 0;
    $i = 0;
    for ( ; $i < $strlen; $i++, $clen++ )
    {
      if ( 160 < ord( substr( $str, $i, 1 ) ) )
      {
        if ( $start <= $clen )
        {
          $tmpstr .= substr( $str, $i, 5 );
        }
        $i++;
      }
      else if ( $start <= $clen )
      {
        $tmpstr .= substr( $str, $i, 1 );
      }
      if ( $start + $len <= $clen )
      {
        break;
      }
    }
    return $tmpstr;
  }
}

 function utf8_substr($str, $start, $length) {  
     $pa="/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|";  
     $pa.="\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";  
     preg_match_all($pa, $str, $t_string);  
     if(count($t_string[0]) - $start > $length) {  
         return join('', array_slice($t_string[0], $start, $length)) . "..";  
     } else {  
         return join('', array_slice($t_string[0], $start, $length));  
     }  
 } 

相关文章:

看完了要说点啥么?