Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove Carriege Returns

Status
Not open for further replies.

bnc123

Technical User
Mar 25, 2001
59
0
0
AU
I get input from the textarea of an HTML form. I store it in a database file. It is normal for people to hit the carriage return (enter) key a couple of times at the end of a paragraph, and start a new one with a blank line inbetween paragraphs.

So far, so good.

I replace the carriage returns \n"; with <br> using the conventional method.

However, you get these people every once in a while, who hit the enter key about 15 times once they have finished typing.

So my question is: How do I retain the carriage returns inbetween paragraphs, but get rid of the carriage returns after the last paragraph?




















 
Sorry Guys,

Kevin's method does not work.

And vanbroo's method gives me the error:
Scalar found where operator expected at post_sug.cgi line 127, near "/^(.*?)\n*$/$1"

 
this works (for me!):-

Code:
[b]#!/usr/bin/perl[/b]

undef $/;
$text = <DATA>;
$/ = "\n";

$text =~ s/\n{2,}//g;

print $text . "|END";

__DATA__
blah blah
blah blah blah
blah blah
blah blah blah
blah blah
blah blah blah

... there are 15 hard returns after the last bit of readable text ;-)


Kind Regards
Duncan
 
before anybody says "but that will delete all double hard returns" - my response is "... so what? it won't hurt - will it!?" :)


Kind Regards
Duncan
 
the OP wants to retain except for the last paragraph, which I assume is the end of the string. My code should work if it's \n (newlines) but if it's \r\n it will need to be changed to:

$text =~ s/(\r\n)+$//;

 
Hi Kevin

Well he's already replaced all the carriage returns with <br>'s - hasn't he?


Kind Regards
Duncan
 
tired.

Code:
[b]#!/usr/bin/perl[/b]

undef $/;
$text = <DATA>;
$/ = "\n";

@lines = split(/\n/, $text);

while ($lines[-1] =~ m/^\s*$/) {
  pop @lines;
}

$text = join("\n", @lines);

print "$text|END";

__DATA__
blah blah
blah blah blah
blah blah
blah blah blah
blah blah
blah blah blah












blah

... and can't think of an easier way than this!?


Kind Regards
Duncan
 
yes, so I am confused why my regular expression isn't doing what the OP wants. I suspect there is something else going on.
 
Well, I tried lots of combinations and here is what worked atlast:

Code:
$text =~ s/(\r\n)+$//;
$text =~ s/\n/<br>/g;
$text =~ s/\r//g;

The first line did not do it by itself, the first and second lines did not do it by themselves. I had to introduce the third line to get it working.

Thanks Kevin, duncdude and vanbroo for helping me out.

 
that makes sense, you would want to remove the trailing \n's first before replacing the rest with <br>.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top