Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

(jeffa) Re: Wordwrap text

by jeffa (Bishop)
on Jul 27, 2001 at 00:06 UTC ( #100123=note: print w/replies, xml ) Need Help??

in reply to Wordwrap text

You should rely on HTML to do this - just specify the width of the column, preferably with a percentage:
<td width="25%">big long sentance . . .</td>
The following _hack_ will work, but it's nasty:
use constant POS => 10; my $str = 'big old long sentance blah blah blah'; my $len; foreach (split(/\s/,$str)) { $len += length; print; ($len % POS) ? print ' ' : print '<br>'; }
Thanks nardo - that's what i get for changing my code from the testing board to the posting board (this cleverness thing just isn't working out for me today). Besides, a ternary operator in void context is evil. . . .
#inside foreach $len += length; print; if ($len > POS) { # thanks nlafferty print '<br>'; $len = 0; } else { print ' '; }



Replies are listed 'Best First'.
Re: (jeffa) Re: Wordwrap text
by nardo (Friar) on Jul 27, 2001 at 00:39 UTC
    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; }
      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.
Re: (jeffa) Re: Wordwrap text
by nlafferty (Scribe) on Jul 27, 2001 at 01:13 UTC
    So i want
    use constant POS => 10; my $str = '$comment'; my $len; foreach (split(/\s/,$str)) { $len += length; print; if ($len > 10) { print '<br>'; $len = 0; } else { print ' '; }
    ? Where does the POS come in?

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2022-06-29 13:56 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (96 votes). Check out past polls.