Page 1 of 1

replace \n with NULL

Posted: Sun Oct 26, 2008 2:03 am
by pcoder
Hi,
I have to find out the \n at the end of the string and replace it with null.
I tried to get it with

Code: Select all

 
$statement = ereg_replace("/[\n]$/","",$statement );
 
But it does not work.
Any suggestions will be highly appreciated.
Thanks

Re: replace \n with NULL

Posted: Sun Oct 26, 2008 3:15 am
by prometheuzz
Hi,

Use preg_replace(...) for this:

Code: Select all

<?php
$text = "line 1 \nline 2\n";
echo "->$text<-\n\n";
$text = preg_replace('/\n$/', '', $text);
echo "->$text<-\n";
?>
Output:

Code: Select all

->line 1 
line 2
<-
 
->line 1 
line 2<-
HTH

Re: replace \n with NULL

Posted: Sun Oct 26, 2008 3:49 am
by GeertDD
Alternatively you can use a built-in PHP string function:

Code: Select all

rtrim($text, "\n");

Re: replace \n with NULL

Posted: Sun Oct 26, 2008 3:50 am
by prometheuzz
GeertDD wrote:Alternatively you can use a built-in PHP string function:

Code: Select all

rtrim($text, "\n");
Seconded.
If I had known of the existence I also would have recommend that in the first place!

Re: replace \n with NULL

Posted: Sun Oct 26, 2008 5:02 am
by pcoder
thanks for the reply guys.