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

Fellow monks,
I'm accepting a textarea from a form and need to replace line breaks in the field with <br> tags for presentation. I've tried my regexp a number of different ways with no success. Here is my code:
my $text = $query->param('text'); $text =~ s/\\r\\n/<br>/; $query->param( -name => 'text', -value => $text );

What am I doing wrong here?

Humbly,
dpatrick

Replies are listed 'Best First'.
Re: Replacing line returns with br
by suaveant (Parson) on Jan 08, 2002 at 00:02 UTC
    Try
    $text =~ s/\r?\n/<BR>/g;
    \\r is trying to replace a slash followed by an r, not a carriage return or whatever... and if it is a unix file it won't have a \r even... not sure but mac files are weird, too...

                    - Ant
                    - Some of my best work - (1 2 3)

      mac   \015
      *nix  \012
      dos   \015\012
      
      So, my way of doing this is s/\015?\012|\015/<br>\n/g.

      2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Re: Replacing line returns with br
by boo_radley (Parson) on Jan 08, 2002 at 00:01 UTC
    $text =~ s/\\r\\n/<br>/;
    This does not do what you expect of it -- you're escaping the second backslash, not the the carriage return/newline.
Re: Replacing line returns with br
by Spudnuts (Pilgrim) on Jan 08, 2002 at 03:34 UTC
    This worked for me, assuming that I understood your problem properly:
    $_ =~ s/$\//<br>$\//g;
Re: Replacing line returns with br
by Nerdry (Initiate) on Jan 08, 2002 at 19:24 UTC
    I've always just done:
    $text =~ s/\n/<BR>/
    Nerdry
      Correct me if I'm wrong, but wont this just replace the first occurrence of \n...I think we need to have the global modifier symbol ('g') at the end of the regexp :
      $text =~ s/\n/<br>/g;
        > $text =~ s/\n/<br>/g;
        This is the expression I use as well. Replacing just the \n assures it works well even if there are no \r around (Unix environment). If there are (Windoze or MAC) I think they can just stay there, but one could get rid of them using:
        $text =~ s/\r//g;
Re: Replacing line returns with br
by johanvdb (Beadle) on Jan 08, 2002 at 15:43 UTC
    Hi, Maybe you can look at the following module to do the job:
    HTML-FromText-1.005.readme

    ... although doing it via a regular expression will grease your perl skills ;-)

    J.
Re: Replacing line returns with br
by LordAvatar (Acolyte) on Jan 09, 2002 at 03:26 UTC

    Try

    $text =~ s/\r\n/<br>/;

    Instead of escaping the \r\n you were escaping the slash \\r...

    -Lord Avatar "A simple truth is but a complicated lie..." -Nietzche

Re: Replacing line returns with br
by jerrygarciuh (Curate) on Jan 09, 2002 at 06:04 UTC
    I have had success with
    $text =~ s/\r\n/<BR>/g;
    HTH
    jg
    _____________________________________________________
    If it gets a little bit out of hand sometimes, don't let it fool you into thinkin' you don't care.TvZ
Re: Replacing line returns with br
by Anonymous Monk on Jan 09, 2002 at 10:13 UTC

    Use

    $text =~ s/\n/<br>/;

    instead of

    $text =~ s/\\r\\n/<br>/;

    Good luck!

Re: Replacing line returns with br
by dpatrick (Scribe) on Jan 12, 2002 at 00:14 UTC
    Thank you to all who responded. I ended up going with the textual representation of the line breaks as opposed to the octal(?) representation, for readability.

    dpatrick