Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

catching newlines from textarea input

by maksl (Pilgrim)
on Feb 28, 2003 at 11:44 UTC ( [id://239393]=perlquestion: print w/replies, xml ) Need Help??

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

dear monks,
i already had a lots of nice input thx to the chat, but the following piece of code is still not working.

the input is passed by an textearea from an user (browser), but the first regex eats all newlines, so there is nothing left for the second one.

i would want to have single linebreaks from the textearea to be turned out in <br> and double linebreaks in <\p><p>

use strict; use CGI; my $query = new CGI; my $text = cleanup_txt( $query->param( 'text' )); print ubb_txt( $text); sub cleanup_txt { my $str = $_[0]; return "" unless ( defined $str ); $str =~ s/[<>"'&|]//gs; } sub ubb_txt { my $txt = shift; return "" unless ( defined $txt); my ($nl) = $txt =~ m{(\cM?\cJ)}; $txt =~ s{$nl$nl}{</p><p>}gs if (defined $nl); $txt =~ s{$nl}{<br>}gs if (defined $nl); }

thx for helping out it's my first *real* post maksl

Replies are listed 'Best First'.
Re: catching newlines from textarea input
by Abigail-II (Bishop) on Feb 28, 2003 at 11:52 UTC
    use strict; use warnings; sub ubb_txt { my $txt = shift; return "" unless ( defined $txt); my ($nl) = $txt =~ m{(\cM?\cJ)}; $txt =~ s{$nl$nl}{</p><p>}gs if (defined $nl); $txt =~ s{$nl}{<br>}gs if (defined $nl); $txt; } my $test = <<'--'; One fish Two fish Red fish Blue fish -- print ubb_txt ($test), "\n"; __END__ One fish<br>Two fish</p><p>Red fish<br>Blue fish<br>

    Seems like the first regexp isn't eating all the newlines.

    Abigail

      yes Abigail-II you are right on this sample, but with the input from the cgi textarea it does not :(
        And, care to show us what the input is from the textarea, or do you believe we are omniscient?

        Because, if the first regex does eat all the newlines, it's a bug in Perl.

        Abigail

Re: catching newlines from textarea input
by robartes (Priest) on Feb 28, 2003 at 11:59 UTC
    You simply forgot to return $txt. A sub returns the result of the last statement by default, in your case this is the substitution. Try this instead:
    #!/usr/local/bin/perl -w use strict; my $txt="This is a line\nThis is a line as well\n\nThis is the last li +ne\n"; print ubb_txt($txt); sub ubb_txt { my $txt = shift; return "" unless ( defined $txt); my ($nl)=$txt =~ m{(\cM?\cJ)}; $txt =~ s{$nl$nl}{</p><p>}gs if (defined $nl); $txt =~ s{$nl}{<br>}gs if (defined $nl); return $txt; } __END__ This is a line<br>This is a line as well</p><p>This is the last line<b +r>
    Your sub was returning the number of substitutions made (i.e. the result of the last substitution).

    CU
    Robartes-

      uups robartes you are right forgot to paste it in, because omited several lines of ubb_txt like $str =~ s/Ü/&Uuml;/igs;

        You may want to look at HTML::Entities instead of hand-rolling all your own entity encodings.

Re: catching newlines from textarea input
by maksl (Pilgrim) on Feb 28, 2003 at 12:49 UTC
    as always looking to the same lines of code i didn't catch my own fault ..
    pff just changed the </p><p> in the second line in wanted <br>:
    $txt =~ s{(?<=[\w&;\\\-:\/])$nl\s*$nl(?=\s*[\w&;\\\-:\/])}{</p><p>}g if ( defined $nl ); $txt =~ s{(?<=[\w&;\\\-:\/])$nl(?=\s*[\w&;\\\-:\/])}{</p><p>}g if ( defined $nl );
    cms does now linebreaks as assumed and wanted :))
    "grand merci" to everybody for their interesting input maksl

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://239393]
Approved by Hofmator
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-19 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found