Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: (jeffa) Re: Wordwrap text

by nardo (Friar)
on Jul 27, 2001 at 00:39 UTC ( #100140=note: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://100140]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2022-05-23 00:26 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (81 votes). Check out past polls.