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; }

Replies are listed 'Best First'.
Re: Re: (jeffa) Re: Wordwrap text
by bastard (Hermit) on Jul 27, 2001 at 02:13 UTC
    This should also work:
    #example 1 $content =~ s/(.{1,$width}) /$1<BR>/g; print $content;
    #example 2 $content =~ s/(.{1,$width}) |([^ ]{$width})/$1$2<BR>/g; print $content;
    The $width represents the largest number of characters each line should have.
    The first example does not break single words longer than the variable $width specifies. The second will.