Link to home
Start Free TrialLog in
Avatar of haast
haast

asked on

Remove carrige return line feed from a string

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 ????????
 
Avatar of Kim Ryan
Kim Ryan
Flag of Australia image

# search and replace CR with nothing
$variable =~ s/\n//;
ASKER CERTIFIED SOLUTION
Avatar of jhurst
jhurst

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
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.
Avatar of ozo
Why do you say that $variable =~ tr/\n/ /; does not seem to work?
Avatar of haast
haast

ASKER

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.