note 17062 deleted from function.wordwrap by jimw
| From: | jimw@php.net | Date: | Fri, 14 Dec 2001 20:39:59 +0000 |
| Subject: | note 17062 deleted from function.wordwrap by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-22777@lists.php.net to get a copy of this message | ||
Here is another solution for
setting a max width of a word
plus a max # of words per line
check it out and if ther are any problems
email me
<pre>
function word_wrap($str,$wordcount,$linebreak,$maxword){
/**************************************
First we cut the big words a parte
**************************************/
$strarray = split(" ",$str);
$strwordcount = count($strarray);
if($strwordcount > $wordcount){
for($i=0;$i<=$strwordcount;$i++){
if(strlen($strarray[$i]) > $maxword){
for($e=0;$e<strlen($strarray[$i]);$e=$e+$maxword){
$strnew .= substr($strarray[$i],$e,$maxword)." ";
}
}
else{
$strnew .= $strarray[$i]." ";
}
}
}
/*************************************
Cut the rows apart
**************************************/
$strarray = split(" ",$strnew);
$strwordcount = count($strarray);
$strnew = "";
if($strwordcount > $wordcount){
$o =0;
for($i=0;$i<=$strwordcount;$i++){
if($o == $wordcount){
$strnew.= $linebreak;
$o = 0;
}
else{
$o++;
$strnew .= $strarray[$i]." ";
}
}
}
return $strnew;
}
$msg = "1sssss 2ssdsf 3asdfsssssssssss
4ssssss22222222222222222222222222222222222222222222222222222222sssssss 5ssssssssdg 6sssssas
7sssssssss 8ssssssssssss 9sssssssssssssssssssssss 10sssssssssss";
echo nl2br(word_wrap($msg,5,"\n",15));
</pre>
this will return this
<pre>
1sssss 2ssdsf 3asdfssssssssss s 4ssssss22222222
222222222222222 222222222222222 222sssssss 5ssssssssdg
7sssssssss 8ssssssssssss 9ssssssssssssss sssssssss 10sssssssssss
</pre>