$str_str="截一字符串,有全角有半角。20个半角换行或者19个半角";
function f_str_rep($str,$num){
$length = strlen($str);
$length_chars=mb_strlen($str,"utf-8");
$new_str="";
if($length>$num){
for($i=0;$i<$length_chars;$i++){
$chars= mb_substr($str,$i,1,"utf-8");
if(strlen($new_str)==$num-1){
if(strlen($chars)>1){
$new_array[] =$new_str;
$new_str=$chars;
}else{
$new_str .=$chars;
}
}elseif(strlen($new_str)==$num){
$new_array[] =$new_str;
$new_str=$chars;
}else{
$new_str .=$chars;
}
}
if($new_str!="")$new_array[] =$new_str;
$last_str = implode("<br>", $new_array);
//print_r($new_array);
}else{
$last_str =$str;
}
return $last_str;}
echo f_str_rep($str_str,"6");