Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Wordwrap text

by nlafferty (Scribe)
on Jul 27, 2001 at 00:01 UTC ( #100118=perlquestion: print w/replies, xml ) Need Help??

nlafferty has asked for the wisdom of the Perl Monks concerning the following question:

How can i take my output(A very long string) from a database and print it in a table so that the string wraps to fill in the given space instead of stretching the cell? Not really sure if this would involve putting a <BR> Tag in after a certain amount of spaces or if there is an easier way.

Replies are listed 'Best First'.
Re: Wordwrap text
by rrwo (Friar) on Jul 27, 2001 at 05:58 UTC

    You can try to specify specific table cell widths in HTML, but there's no guarantee that the browser will respect it. Sometimes some browsers just don't handle it well.

    A hack is to use Text::Wrap and convert newlines to "br" tags. It's not elegant, but it's the only way to ensure the text is shown as you want it (99% of the time, anyway).

(jeffa) Re: Wordwrap text
by jeffa (Bishop) on Jul 27, 2001 at 00:06 UTC
    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 ' '; }


      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.
      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?
Re: Wordwrap text
by John M. Dlugosz (Monsignor) on Jul 27, 2001 at 00:06 UTC
    Fix the width of the table column, and the text will wrap instead of automatically expanding. Try the htmlcompendium.
      This works only for text that contains certain characters. (Whitespace, -, and probably a few others) I'll call these breakable characters. (certain browsers may break without these characters anyway, but i've seen many that don't).

      The Chatterbox to the right uses the approach of forcing a space every x characters, probably every x characters after the last breakable character. This will then let the previously mentioned specification of table widths work.

      Or, just read the neat, handy-dandy-doody documentation that comes with CGI, as it explains how to do that.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2022-06-25 20:58 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (83 votes). Check out past polls.