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

Hi, I need to format a paragraph that I retrieve from a MySQL db column thats MEDIUMTEXT. I need to print part of this paragraph and have it so it looks neat on the web page.

The problem is that I can not get this paragraph to have a line break after each paragraph or sentence that has a line break after.

For ex: Team: Dallas Mavericks
Date: Feb 1, 2006
Time: 8:00
Opponent: Memphis

Details: These two teams will be facing off once again this season as the Dallas Mavericks look to tie up the series.
--------------

the above will end up looking like this on my browser:

Team: Dallas Mavericks Date: Feb 1, 2006 Time: 8:00 Opponent: Memphis Details: These two teams will be facing off once again this season as the Dallas Mavericks look to tie up the series.

How could I have it so it displays properly with line breaks.

Thanks,
Lex

Replies are listed 'Best First'.
Re: Formatting paragraph
by ikegami (Patriarch) on Feb 02, 2006 at 05:01 UTC
    You need to convert it from plain text to HTML. The following is a start:
    foreach ($text) { s/&/&amp;/g; s/</&lt;/g; s/>/&gt;/g; s/\n/<br>/g; }

    It's not perfect. For example, it won't stop the browser from collapsing multiple spaces into one. For that, you'd have to use something like the following:

    foreach ($text) { s/&/&amp;/g; s/</&lt;/g; s/>/&gt;/g; $_ = "<pre>$_</pre>"; }

    You have to restrict yourself to monospace fonts (like Courier New) for <pre> to work properly (but <pre> text should default to a monospace font).

      How could I have it so it displays properly with line breaks.
      You need to convert it from plain text to HTML

      I wonder how the OP was able to deliver his SoPW post -- completely well-formatted, making his point -- without being HTML aware?

      You need to convert it from plain text to HTML.

      Gosh! I was having a hard time trying to make sense of the OP's request when I read your reply: I keep forgetting that most often here people asks about web development without even clearly saying so...

        The amazing thing is that he posted a completly off topic node, and NOBODY flamed him.

        This is not your standard news group!

Re: Formatting paragraph
by clinton (Priest) on Feb 02, 2006 at 10:33 UTC
    What about:
    # First encode possible HTML, because the text is # supposed to be plain text use HTML::Entities(); $text = HTML::Entities::encode($text); # Convert Windows or Mac line terminators to Unix single \n $text =~ s/\r\n|\n\r/\n/g; # Remove leading and trailing white space $text =~ s/^\s*//; $text =~ s/\s*$//g # Convert all double line breaks to para tags $text =~ s|\n\s*\n|</p><p>|g # Convert any single line breaks to br tags $text =~ s|\n\s*|<br />|g # If text is more than just white space, add opening and # closing para tags $text = "<p>$text</p>" if $text;
      I'm sorry but how was my question OT? I'm pretty sure Perl is required to do what I'm doing. is it not? The two replies all had to do with Perl and html, so in the end its still on topic since it deals with Perl.

        Yes, the significative replies you got all have to do with Perl and HTML. The question, though, had to do with neither. Actually no perl code is shown nor Perl is mentioned at all, although being here suggests beyond any reasonable doubt that you're using perl to get your data out of the db. OTOH no mention at all is being made of HTML either. People made a reasonable guess, and it turned out to be correct. Due to my cultural backround -or lack of imagination- it was not just as easy for me to get at the same guess. So yes: your question had to do with Perl, but it was about "formatting pure text for display in a web page". Or something that. Wasn't it?