Finding newline chars

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

Moderator: General Moderators

Post Reply
User avatar
MathewByrne
Forum Commoner
Posts: 38
Joined: Sat Mar 27, 2004 9:49 pm
Location: Australia

Finding newline chars

Post by MathewByrne »

Hi,
I havn't really gotten to much into String matching and Regex but I was just wondering if there was a simple way of finding if a string has a newline character at the end of it (\n).
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Try this

Code: Select all

$string = "foo
bar
"; //Ends on newline

if (preg_match('/\n$/', $string)) {
   //End on newline
} else {
   //End some other way
}
Post Reply