Link to home
Start Free TrialLog in
Avatar of mhouldridge
mhouldridge

asked on

nl2br in Perl? - Need to format output and add newlines

Experts,

I am outputting a longtext field from my db to html.  I need to show line breaks as the output is one continuous string.

I know in PHP you can use nl2br which adds <br> however I cant find a function in Perl to do this.

Please help.
Avatar of nitinsawhney
nitinsawhney
Flag of India image

In perl you do it with power of regex string replacement

$longTextFile =~ s/n/<br>/g;

Here longTextFile is the scalar containing your output.

Cheers
ASKER CERTIFIED SOLUTION
Avatar of nitinsawhney
nitinsawhney
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Store each line in a variable and add <br> at the end using regular expression

$ine=~s/(.)+/$1<br>/g;
 
now print each line in a HTML file
Avatar of mhouldridge
mhouldridge

ASKER

Hi,

Thanks for the comments.  How would I store each line as a variable from a longtext field within a database.  I could understand how this would be done from a text file, reading line by line, but I dont understand how I would read a line of a longtext field?
Sorted now. thanks!