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

So I've got a <INPUT type="textarea" name="message">.
However, when I use CGI to retrieve the contents, all the newlines (%0A%0D) have turned into spaces???
I've tried s/space/newline/g, but this cant differentiate between real spaces and newline-space conversions. Any ideas?

Thanks Monks.

Replies are listed 'Best First'.
Re: Newlines in textarea w/ CGI.pm
by belg4mit (Prior) on Nov 18, 2001 at 22:57 UTC
    First it seems you're using HTML 4? (UPDATE:This explains a lot from jcwren in ChatterBox) So this might not work:

    for <textarea> there is a Netscape 2 extension the wrap attribute.

    Depending on who you consult the values may either be: OFF|HARD|SOFT or PHYSICAL|VIRTUAL; I've always used the latter.

    --
    perl -p -e "s/(?:\w);([st])/'\$1/mg"

      Ehhmmm... sorry, but that doesn't make work either! I tried all 5 options. Have a look at this. It takes the contents of the textarea and prints them. Youll notice that between each newline you use in the texarea, a space is printed. WTF?

        Err... I'm wondering about your assumptions now--when I view the source of the response page, I get the newlines back just as I submitted them. You are aware that HTML formats text with all whitespace treated equally, aren't you? That is, all runs of whitespace of any kind (\s+) are condensed to a single space (just as you describe, in fact). If you want to preserve the newlines, use <PRE>preformatted text</PRE> (in CGI.pm, that's pre($text)).

        If you are in fact aware of this, I apologize, but in that case I think you're looking at a browser issue--I'm getting back exactly the results I'd expect from your code, using IE5/Mac.



        If God had meant us to fly, he would *never* have given us the railroads.
            --Michael Flanders

        Try this, since you are using (based on jcwren's link, an IE3 specific syntax with <input type="textarea">

        <textarea wrap=physical></texarea>

        --
        perl -p -e "s/(?:\w);([st])/'\$1/mg"

Re: Newlines in textarea w/ CGI.pm
by tachyon (Chancellor) on Nov 19, 2001 at 16:55 UTC

    ChemBoy is right. View the Page Source to see that the newlines are maintained it is just that they do not render as you expect. If you want to display the text input from a text area run it though this sub:

    sub escapeHTML { my ( $escape, $whitespace ) = @_; return undef unless defined $escape ; # make the essential escapes $escape =~ s/&/&amp;/g; $escape =~ s/"/&quot;/g; $escape =~ s/</&lt;/g; $escape =~ s/>/&gt;/g; # these next optional escapes make text look the same when rendere +d in HTML if ( $whitespace ) { $escape =~ s/\t/ /g; # tabs to 4 sp +aces $escape =~ s/( {2,})/"&nbsp;" x length $1/eg; # whitespace e +scapes $escape =~ s/\n/<br>\n/g; # newlines to +<br> } return $escape; }

    The first argument is the text to HTML escape. If the optional second argument is supplied a number of whitespace modifications are made with the net result that print escapeHTML( $string, 1 ); will render in a browser exactly as you expect. If you use <pre> tags then you don't need the whitespce escapes but you do need the other part.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Newlines in textarea w/ CGI.pm
by Washizu (Scribe) on Nov 19, 2001 at 18:13 UTC

    If you are still having trouble with this one, why not try this solution?

    1. Figure out how many characters are in a line of your text area by using the length of the textarea anf font size.
    2. When the data is passed in, insert line breaks in the appropriate places using a regex similar to this:
      my $length = 20; # Sample length text area line while ($textData =~ s/(.{$length})/$1\n/gcs) {}

    Note: This isn't a particularly good solution, just an odd one.

    -----------------------------------
    Washizu
    The best offense is a good offense.