Solved

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

Posted on 2004-10-16
5
29,240 Views
Last Modified: 2013-12-25
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
Comment
Question by:Sparky-Plug
  • 2
  • 2
5 Comments
 
LVL 15

Accepted Solution

by:
nicholassolutions earned 65 total points
ID: 12331383
Try this:
$input =~ s/\r?\n/<br>/g;
0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 65 total points
ID: 12331916
$input =~ s/[\r\n]/<br>/g;
0
 

Author Comment

by:Sparky-Plug
ID: 12331968
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 12331976
> .. difference between New Line and Carriage Return?
read verbatim :-)
0
 
LVL 15

Expert Comment

by:nicholassolutions
ID: 12333263
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Ready Made PHP Scripts 5 62
Powershell / new-object PSObject rows 2 210
running netsh advfirewall set rule on multiple computers 3 36
Disk Ram Peak 1 33
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now