Solved

Remove new lines when printing to file

Posted on 2004-08-26
4
160 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
[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
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
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 fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

615 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