Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Remove new lines when printing to file

Posted on 2004-08-26
4
Medium Priority
?
162 Views
Last Modified: 2013-12-25
I have this code but when I save the data to a file it keeps the new line parameter. How can I make it so that it stays on one line and prints <br> instead?

For example this script might print to file;
Hello
this
is a
test

but I want it to print;
Hello<br>this<br>is a<br>test

$buffer=$ENV{'QUERY_STRING'};
$buffer =~ tr/+/ /;
$buffer =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$buffer =~ s/<!--(.|\n)*-->/ /g;
$buffer =~ tr/\\|[|]|<|!|"|$|{|}|*|#|'|>|||;|%/ /;
@pairs = split(/&/,$buffer);
foreach $pair(@pairs){
($key,$value)=split(/=/,$pair);
$formdata{$key}.="$value";
}

$text=$formdata{'text'};

open(INFO, ">>formdata.txt");
print INFO "$text\n";
close (INFO);

Many thaks,
S-P
0
Comment
Question by:Sparky-Plug
  • 2
4 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 11908230

#!/usr/bin/perl
use strict;
use CGI;

my $q = new CGI;
my $text = $q->param('text');
$text =~ s/\n/<br>/g;

open INFO, ">>formdata.txt" or die "Can not append to formdata.txt $!\n";
print INFO "$text\n";
close INFO;
0
 

Author Comment

by:Sparky-Plug
ID: 11910405
Thanks, that works well. It doesn't print it all on the same line but when read and printed as part of an html file it works how I want it.

(formdata.txt;
hello
<br>this
<br>is a
<br>test
)

I'm guessing it strips a hidden new line character.

Many Thanks,
S-P
0
 
LVL 6

Expert Comment

by:nitinsawhney
ID: 11924739
Hi Sparky,
Only a small changes in the code will solve ur problem..

#!/usr/bin/perl
use strict;
use CGI;

my $q = new CGI;
my $text = $q->param('text');
$text =~ s/\n/\n<br>/g;  #replaced \n with \n<br> so that <br> comes in next line
$text =~ s/<br>$//g;     # removed extra <br> whic will come after the last text.

open INFO, ">>formdata.txt" or die "Can not append to formdata.txt $!\n";
print INFO "$text\n";
close INFO;

Hope this solves ur problem.
Cheers!!
Nitin
0
 
LVL 48

Accepted Solution

by:
Tintin earned 320 total points
ID: 11927698
Try changing

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

to

$text =~ s/[\n\r]/<br>/g;
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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
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 …
Suggested Courses

916 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