Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 879589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:57:26+00:00 2026-05-15T11:57:26+00:00

I have a <textarea> for user input, and, as they are invited to do,

  • 0

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… 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T11:57:26+00:00Added an answer on May 15, 2026 at 11:57 am
    #!/usr/bin/perl
    
    use strict; use warnings;
    
    use Socket qw( :crlf );
    
    my $text = "a${CR}b${CRLF}c${LF}";
    
    $text =~ s/$LF|$CR$LF?/<br>/g;
    
    print $text;
    

    Following up on @daxim’s comment, here is the modified version:

    #!/usr/bin/perl
    
    use strict; use warnings;
    use charnames ':full';
    
    my $text = "a\N{CR}b\N{CR}\N{LF}c\N{LF}";
    
    $text =~ s/\N{LF}|\N{CR}\N{LF}?/<br>/g;
    
    print $text;
    

    Following up on @Marcus’s comment here is a contrived example:

    #!/usr/bin/perl
    
    use strict; use warnings;
    use charnames ':full';
    
    my $t = (my $s = "a\012\015\012b\012\012\015\015c");
    $s =~ s/\r?\n/<br>/g;
    
    $t =~ s/\N{LF}|\N{CR}\N{LF}?/<br>/g;
    
    print "This is \$s: $s\nThis is \$t:$t\n";
    

    This is a mismash of carriage returns and line feeds (which, at some point in the past, I did encounter).

    Here is the output of the script on Windows using ActiveState Perl:

    C:\Temp> t | xxd
    0000000: 5468 6973 2069 7320 2473 3a20 613c 6272  This is $s: a<br
    0000010: 3e3c 6272 3e62 3c62 723e 3c62 723e 0d0d  ><br>b<br><br>..
    0000020: 630d 0a54 6869 7320 6973 2024 743a 613c  c..This is $t:a<
    0000030: 6272 3e3c 6272 3e62 3c62 723e 3c62 723e  br><br>b<br><br>
    0000040: 3c62 723e 3c62 723e 630d 0a              <br><br>c..
    

    or, as text:

    chis is $s: a<br><br>b<br><br>
    This is $t:a<br><br>b<br><br><br><br>c
    

    Admittedly, you are not likely to end up with this input. However, if you want to cater for any unexpected oddities that might indicate a line ending, you might want to use

    $s =~ s/\N{LF}|\N{CR}\N{LF}?/<br>/g;
    

    Also, for reference, CGI.pm canonicalizes line-endings this way:

    # Define the CRLF sequence.  I can't use a simple "\r\n" because the meaning
    # of "\n" is different on different OS's (sometimes it generates CRLF, sometimes LF
    # and sometimes CR).  The most popular VMS web server
    # doesn't accept CRLF -- instead it wants a LR.  EBCDIC machines don't
    # use ASCII, so \015\012 means something different.  I find this all 
    # really annoying.
    $EBCDIC = "\t" ne "\011";
    if ($OS eq 'VMS') {
      $CRLF = "\n";
    } elsif ($EBCDIC) {
      $CRLF= "\r\n";
    } else {
      $CRLF = "\015\012";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a textarea in which the user enters the following data: Paul:Nine, Rome
I have a textarea, where the user can add their comments. Now after adding
I have a textarea that is being dynamically reloaded as user input is being
I have a textarea field on my webpage. I am accepting the user input,
I have a website with a textarea that prompts users to input a URL
I have an app where a user can input text in a textarea. I
I have a textarea input on my site where the user can input information,
I have an input form with two textareas allowing a user to type in
I have textarea in my site(link shortening site) i want to user enter some
I have an HTML textarea element. I want to prevent a user from entering

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.