http://www.perlmonks.org?node_id=410494

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

I'm have a form field that I am submitting to a Perl CGI script, I want to replace all the new lines with <br> so when I redisplay the form submission in html that it formats the new lines correctly.
I have been trying each one of these because I'm not sure if it is a CR, LF, or CRLF:
$INPUT{'comments'} =~s/\n/<br>/g; $INPUT{'comments'} =~s/\r/<br>/g; $INPUT{'comments'} =~s/\r\n/<br>/g;
I can't seem to figure out why I can't insert any of these HTML tags into my string. How do I replace the new lines that are in the multiline text box with the HTML Break Tags?

Replies are listed 'Best First'.
Re: Replacing new lines in multi line text box form fields with <br>.
by Zaxo (Archbishop) on Nov 26, 2004 at 02:32 UTC

    How are you setting %INPUT? What is under its 'comments' key before substitution? What is there after substitution?

    You may want to reorder your substitutions so that the CRLF one is first. Your first two should give two <br /> tags to a CRLF pair and prevent the third from matching.

    After Compline,
    Zaxo

      Okay, good catch, I went to look how %INPUT was created and I saw a line where supposedly harmful characters were being stipped out, /n was one that was stripped.

      I deleted that line and now this works

      $INPUT{'comments'} =~s/\n/<br>/g;

      Thanks, dumb mistake.

        You must be careful, if input comes from a TEXTAREA, your text will contain a CRLF combination for every end-of-line. You're better off replacing those with plain newlines, "\n", before you insert them in a database, for example. Or before feeding it back into the HTML for the next rendered page.
        s/\x0D\x0A/\n/g

        And don't forget to HTML-escape your string, when inserting it into the HTML, both for the TEXTAREA for edit and in the plain HTML for view, the former before you insert your <BR> tags — or you might end up escaping your freshly inserted tags, too.

        Oh, and it'd still be nice if you kept the newlines. For example:

        s/$/<BR>/mg;
        or
        s/\n/<BR>\n/g;
Re: Replacing new lines in multi line text box form fields with <br>.
by tachyon (Chancellor) on Nov 26, 2004 at 06:50 UTC
    Use a character class to deal with LF CR and CRLF line endings. A standard newline after the
    maintains readability.
    $INPUT{'comments'} =~ s/[\r\n]+/<br>\n/g;
      $INPUT{'comments'} =~ s/[\r\n]+/<br>\n/g;

      That isn't a good pattern to use, because it means that two line-breaks in the input, such as used to indicate a paragraph break, become only one in the output.

      Smylers

        You're right. s/\r\n|\r|\n/<br>\n/g is better.

        cheers

        tachyon

Re: Replacing new lines in multi line text box form fields with <br>.
by davorg (Chancellor) on Nov 26, 2004 at 11:00 UTC

    Lots of people giving you variations on the theme of how to make the changes you asked for, but no-one stepping back and asking what you're trying to achieve.

    Changing newlines to <br> tags is a display issue. You should only be making changes like this to a copy of the data that it being displayed to the user in a browser. If you're storing the data somewhere for processing later then you should store it in its original format.

    As an example of why this approach it useful consider the change from HTML to XHTML. Currently it looks like you are displaying the data as HTML (as <br> is an HTML tag), but eventually you are going to want to display the data as XHTML instead. The equivalent tag in XHTML is <br/>.

    If you're storing the original version of the data then you can decide what format you are planning to display it in at the time when you display it. If you save it as HTML then you're making the decision that you're always going to display it as HTML. Which may not be true in two years time.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      I'm taking the data and storing it a database as text. So from what you're saying I'd take the data as I get it out of my database and then replace the /n with what ever the equilivent tag is for what I'm displaying.

        Yes. That's exactly what I'm saying.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: Replacing new lines in multi line text box form fields with <br>.
by fglock (Vicar) on Nov 26, 2004 at 03:10 UTC

    How about using <pre> tags?

      The problem with that is two-fold: <pre> tags don't let text wrap, and they force a typeface change. This, for many web design projects, is suboptimal at best.

      - apotheon
      CopyWrite Chad Perrin

Re: Replacing new lines in multi line text box form fields with <br>.
by wfsp (Abbot) on Nov 26, 2004 at 09:16 UTC
    On processing form data, w3c says:

    "Line breaks are represented as "CR LF" pairs (i.e., `%0D%0A')."

    This can catch you out if you test the script on one platform (win) and run it on another (unix) because newlines are treated differently.

    It looks like tachyon's reply will catch both cases.

Re: Replacing new lines in multi line text box form fields with <br>.
by mkirank (Chaplain) on Nov 26, 2004 at 10:48 UTC
    $INPUT{'comments'} =~ s/\r\n?|\n/<br>/g;