Solved

Remove new lines when printing to file

Posted on 2004-08-26
4
154 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 80 total points
ID: 11927698
Try changing

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

to

$text =~ s/[\n\r]/<br>/g;
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio‚Ķ
This article will show, step by step, how to integrate R code into a R Sweave 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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 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

15 Experts available now in Live!

Get 1:1 Help Now