Solved

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

Posted on 2004-10-16
5
29,245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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.
This article will show, step by step, how to integrate R code into a R Sweave document
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

733 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