in reply to Replacing new lines in multi line text box form fields with <br>.

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

  • Comment on Re: Replacing new lines in multi line text box form fields with <br>.

Replies are listed 'Best First'.
Re^2: Replacing new lines in multi line text box form fields with <br>.
by awohld (Hermit) on Nov 26, 2004 at 02:38 UTC
    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;