//无乱码截取字符串,中英文混合 function left($str, $len, $charset="utf-8"){ //如果截取长度小于等于0,则返回空 if( !is_numeric($len) or $len <= 0 ){ return ""; } //如果截取长度大于总字符串长度,则直接返回当前字符串 $sLen = strlen($str); if( $len >= $sLen ){ return $str; } if ( strtolower($charset) == "utf-8" ){ $len_step = 3; }else{ $len_step = 2; } $len_i = 0; $substr_len = 0; for( $i=0; $i < $sLen; $i++ ){ if ( $len_i >= $len ) break; if( ord(substr($str,$i,1)) > 0xa0 ){ $i += $len_step - 1; $substr_len += $len_step; }else{ $substr_len ++; } $len_i ++; } $result_str = substr($str,0,$substr_len ); return $result_str; }