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

I've a multi paragraph section of text stored in a database. Using DBI I want to grab the entive paragraph an format it for HTML viewing. Right now I'm just tyring to handle the paragraphs(newlines). Although the text has several paragraphs, since it currently lacks HMTL tags, it's displayes as one long blob instead. Here is a sippet:
my $sql = qq(SELECT * from $status WHERE ticket_id=?); my $sth = $dbh->prepare($sql); $sth->execute($ticket); my $record = $sth->fetch; for (@$record[1..9]) { $_ =~ s/($change)/<font color=red>$1<\/font>/i; $_ =~ s/^\s.*/<br><br>/g; }
The above successfully tags a search string in red. However it fails to match any blank or newline. Funny, the HMTL source shows the blank lines. I appreciate any help.

2005-03-11 Edited by Arunbear: Changed title from 'Adding HMTL tags to plain text'

Replies are listed 'Best First'.
Re: Adding HTML tags to plain text
by saintmike (Vicar) on Mar 10, 2005 at 19:06 UTC
    To search for two or more newlines and replace them by <p>\n, use this:

    $data =~ s/\n{2,}/<p>\n/g;

    For a more general approach of turning plain text into HTML, check out HTML::TextToHTML.

      That did it. Thanks.