• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 29269
  • Last Modified:

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.
0
Sparky-Plug
Asked:
Sparky-Plug
  • 2
  • 2
2 Solutions
 
nicholassolutionsCommented:
Try this:
$input =~ s/\r?\n/<br>/g;
0
 
ahoffmannCommented:
$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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now