这个例子可以很好的处理换行,我们知道一个长的英文单词在末行位置不够是会自动起一行,如何处理这种情况,看看这个例子--teaman
function textwrap ($String, $breaksAt = 78, $breakStr = "n", $padStr="") {
$newString="";
$lines=explode($breakStr, $String);
$cnt=count($lines);
for($x=0;$x<$cnt;$x++){
if(strlen($lines[$x])>$breaksAt){
$str=$lines[$x];
while(strlen($str)>$breaksAt){
$pos=strrpos(chop(substr($str, 0, $breaksAt)), " ");
if ($pos == false) {
break;
}
$newString.=$padStr.substr($str, 0, $pos).$breakStr;
$str=trim(substr($str, $pos));
}
$newString.=$padStr.$str.$breakStr;
}
else{
$newString.=$padStr.$lines[$x].$breakStr;
}
}
return $newString;
} // end textwrap()
|