CGI / Perl remove new line character when printing to file.

I have an HTML textarea box that I want to save the input from into a text file.

Instead of printing each 'Enter /return' on a new line I want it on the same line with <br> printed instead.

I am using this at the moment but it doesn't quite work how I'd like;

$input =~ s/\n/<br>/g;

Example:
Input:

This
is

input.

Currently saves as:
This
<br>is
<br>
<br>input.

But I want it like this:

This<br>is<br><br>input.



If you get what I mean, is this possible?

Many Thanks, S-P.
Sparky-PlugAsked:
Who is Participating?
 
nicholassolutionsConnect With a Mentor Commented:
Try this:
$input =~ s/\r?\n/<br>/g;
0
 
ahoffmannConnect With a Mentor Commented:
$input =~ s/[\r\n]/<br>/g;
0
 
Sparky-PlugAuthor Commented:
Many Thanks, Both work equally well. Points will be halved between you.

Incidentally, what is the difference between New Line and Carriage Return?


Thanks again, S-P.
0
 
ahoffmannCommented:
> .. difference between New Line and Carriage Return?
read verbatim :-)
0
 
nicholassolutionsCommented:
carraige return \r means 'go back to the beginning of the line'
newline \n means 'go to a newline'

have a look here for a full explanation:
http://en.wikipedia.org/wiki/Newline
http://www.cdf.utoronto.ca/~csc209h/winter/tut/line-endings.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.