http://qs321.pair.com?node_id=100140


in reply to (jeffa) Re: Wordwrap text
in thread Wordwrap text

That only wraps a line if it ends at a multiple of the boundary, it doesn't prevent a word from going past it (you need to check if the current word is going to go past the boundary before printing it). On a $str of 'George Washington was the first president of these United States' it won't insert a <br> despite there being significantly more than 10 chars in it. This loop should do it:
foreach (split(/\s/, $str)) { if($len + 1 + length > POS && $len) { $len = 0; print "\n"; } elsif($len) { print ' '; $len++; } print; $len += length; }