Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
-
pcoder
- Forum Contributor
- Posts: 230
- Joined: Fri Nov 03, 2006 5:19 am
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
-
prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
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
-
GeertDD
- Forum Contributor
- Posts: 274
- Joined: Sun Oct 22, 2006 1:47 am
- Location: Belgium
Post
by GeertDD »
Alternatively you can use a built-in PHP string function:
-
prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Post
by prometheuzz »
GeertDD wrote:Alternatively you can use a built-in PHP string function:
Seconded.
If I had known of the existence I also would have recommend that in the first place!
-
pcoder
- Forum Contributor
- Posts: 230
- Joined: Fri Nov 03, 2006 5:19 am
Post
by pcoder »
thanks for the reply guys.