Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert various user-inputted line break characters to <br> using Perl?

Tags:

html

perl

I have a <textarea> for user input, and, as they are invited to do, users liberally add line breaks in the browser and I save this data directly to the database.

Upon displaying this data back on a webpage, I need to convert the line breaks to <br> tags in a reliable way that takes into consideration to \n's the \r\n's and any other common line break sequences employed by client systems.

What is the best way to do this in Perl without doing regex substitutions every time? I am hoping, naturally, for yet another awesome CPAN module recommendation... :)

like image 683
Marcus Avatar asked Dec 06 '22 02:12

Marcus


2 Answers

There's nothing wrong with using regexes here:

s/\r?\n/<br>/g;
like image 142
Ether Avatar answered Dec 07 '22 14:12

Ether


Actually, if you're having to deal with Mac users, or if there still happens to be some weird computer that uses form-feeds, you would probably have to use something like this:

$input =~ s/(\r\n|\n|\r|\f)/<br>/g;
like image 29
mmirate Avatar answered Dec 07 '22 14:12

mmirate