i found this function somewhere and tweaked it a little, maybe this helps as well..
function wraplines($oldstr, $wrap)
{
// incoming: $oldstr is the string intended to be wordwrapped
// $wrap is how many lines (int value)
// returns a string, nicely wordwrapped
# we expect the following things to be newlines:
$oldstr=str_replace("<br>","\n",$oldstr);
$oldstr=str_replace("<BR>","\n",$oldstr);
$newstr = ""; $newline = "";
# Add a temporarary linebreak at the end of the $oldstr.
# We will use this to find the ending of the string.
$oldstr .= "\n";
do
{
# Use $i to point at the position of the next linebreak in $oldstr!
# If a linebreak is encountered earlier than the wrap limit, put $i there.
if (strpos($oldstr, "\n") <= $wrap)
{
$i = strpos($oldstr, "\n");
}
# Otherwise, begin at the wrap limit, and then move backwards
# until it finds a blank space where we can break the line.
else
{
$i = $wrap;
while (!ereg("[\n\t ]", substr($oldstr, $i, 1)) && $i > 0)
{
$i--;
}
}
# $i should now point at the position of the next linebreak in $oldstr!
# Extract the new line from $oldstr, including the
# linebreak/space at the end.
$newline = substr($oldstr, 0, $i+1);
# Turn the last char in the string (which is probably a blank
# space) into a linebreak.
if ($i!=0) $newline[$i] = "\n";
# Decide whether it's time to stop:
# Unless $oldstr is already empty, remove an amount of
# characters equal to the length of $newstr. In other words,
# remove the same chars that we extracted into $newline.
if ($oldstr[0] != "")
{
$oldstr = substr($oldstr, $i+1);
}
# Add $newline to $newstr.
$newstr .= $newline;
# If $oldstr has become empty now, quit. Otherwise, loop again.
} while (strlen($oldstr) > 0);
# Remove the temporary linebreak we added at the end of $oldstr.
$newstr = substr($newstr, 0, -1);
return $newstr;
}
probably does the same thing as vincents, however.