[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Remove carrige return line feed from a string

Posted on 1999-11-07
5
Medium Priority
?
3,258 Views
Last Modified: 2010-10-05
I have a $variable received from a HTML textarea form. EG:

This
is
a
test

I want to remove the newlines (carrige return line feeds)from it, so that it looks like:
This is a test
I have tried $variable =~ tr/\n/ /;
BUT IT DOES NOT SEEM TO WORK ????????
 
0
Comment
Question by:haast
5 Comments
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 2190238
# search and replace CR with nothing
$variable =~ s/\n//;
0
 
LVL 8

Accepted Solution

by:
jhurst earned 400 total points
ID: 2190294
Actually I would be happy for haast to get the points - he is so near.  But you need:
$variable =~ s/\n//g; #globally replace all line-feeds with nothing
$variable =~ s/\r//g; #globally replace all carriage-returns with nothing
0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 2190308
Global flag is not needed as the sample data only conains one CR per line.
\n is the new line delimiter for your operating system, meaning LF for Unix or CR/LF for Windows/DOS, there should be no need to explicitly remove carriage returns.

jhurst, the normal etiquitte here is to submit answers as comments and let the questioner decide which ones to accept.
0
 
LVL 85

Expert Comment

by:ozo
ID: 2190334
Why do you say that $variable =~ tr/\n/ /; does not seem to work?
0
 

Author Comment

by:haast
ID: 2190765
teraplane thank you for your suggestion (it did not solve the problem), jhurst is 100% correct the g flag is ESSENTIAL and also besides the =~ s/\n// the =~ s/\r// is ESSENTIAL too.

And I concur with the comment on etiquitte, however, I do understand enthusiasm, so let us all learn from this.

Best Regards,
haast.

PS: ozo you are not forgotten as a potential helper as well.

Thank you ALL.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

591 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