in reply to Newlines in textarea w/ CGI.pm
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/&/&/g; $escape =~ s/"/"/g; $escape =~ s/</</g; $escape =~ s/>/>/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,})/" " 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
|
|---|