replace \n with NULL

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

replace \n with NULL

Post 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
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: replace \n with NULL

Post 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
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: replace \n with NULL

Post by GeertDD »

Alternatively you can use a built-in PHP string function:

Code: Select all

rtrim($text, "\n");
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: replace \n with NULL

Post 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!
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: replace \n with NULL

Post by pcoder »

thanks for the reply guys.
Post Reply