Solved

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

Posted on 2004-10-16
5
29,238 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
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.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 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

18 Experts available now in Live!

Get 1:1 Help Now