Link to home
Start Free TrialLog in
Avatar of Sparky-Plug
Sparky-Plug

asked on

Remove new lines when printing to file

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
Avatar of Tintin
Tintin


#!/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;
Avatar of Sparky-Plug

ASKER

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
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
ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial