Solved

Remove new lines when printing to file

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
size of file 1 37
add if statement powershell 8 58
Powershell - Setting GPO 8 50
Open new WPF windows - powershell 4 39
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

751 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