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

I have a form with a textarea, and people are copying & pasting/typing in entries that contain hard returns. This is then printed into a text file. When the file is accessed, the hard returns mess up reading from the file. I tried to catch \r & \n using
$blah =~ s/\r/<br>/;
&
$blah =~ s/\n/<br>/;
but these still miss a good number of the returns. Is there any way I can fix this?

Replies are listed 'Best First'.
Re: catching \n
by tedrek (Pilgrim) on Aug 07, 2003 at 01:20 UTC

    You are probably looking for /g, substitute globally. Your example will only get the first \r and \n in $blah. Just use something like this which will also handle DOS style returns

    $blah =~ s/\r?\n/<br>/g;

      Or, if you want to be really portable, you could use s/\r?\n|\n?\r//g. That should catch most every platform's newline style, IIRC.

      bbfu
      Black flowers blossom
      Fearless on my breath

        If the web client (browser) doesn't use ASCII "\r\n"1 for newline in the text it sends to you, then the web client is broken, IMHO.

        1Note that this is not ambiguous. I know that "\r\n" on old Macs results in ASCII "\n\r", but that is because those Macs are near-ASCII systems despite them claiming to be ASCII systems. q-:

                        - tye
Re: catching \n
by graff (Chancellor) on Aug 07, 2003 at 02:58 UTC
    If you don't mind having multiple consecutive line breaks (i.e. empty lines) collapsed into a single <br> tag, you could do:
    $blah =~ s/[\r\n]+/<br>/g;
    (Just because I don't recall off-hand...
    I'd like to test whether there's visible difference...

    between single and double <br> tags...
    Yup, there is.)
Re: catching \n
by TheDoc (Initiate) on Oct 07, 2003 at 01:29 UTC
    This doesn't have to deal with \n.... but is there an easy way to find any punctuation in a sentence and rip it out? or would I have to type in ?|.|,| and whatnot in the regex?
      ok so I just said
      unless ($msg =~ /[a-z]|[A-Z]|[0-9]/g) { $msg = "a"; }
      and that seemed to fix the problem... so false alarm I guess