Page 1 of 1

Can't make PHP eregi acknolwedge that newlines are ok

Posted: Mon Mar 21, 2005 1:16 pm
by kirilisa
I made a little function to test if some string is 'alpha extended". The function is below, and should give the ok on strings that contain:

any letters, any numbers, / . , ( ) \ # % & - _ and any whitespace.

public static function isAlphaExtended($s) {
return eregi('^[\sA-Za-z0-9/\.\(\), \\#%&-_]+$', $s);
}

The problem is, this fuction tells you that your string is no good if there is a carriage return. I put in \s to indicate any whitespace ok, I even tried getting rid of the \s and putting in \r\n instead since in the past php hasn't always been ok with those 'combo' regex things, but no dice. No matter what it will not accept my newline.

How do I eregi for a newline? I tried making a tiny function that only matches whitespace and new lines but that doesnt' work either!

public static function isAlphaExtended($s) {
return eregi('^[\n\r ]+$', $s);
}

Posted: Mon Mar 21, 2005 1:44 pm
by feyd
  1. ereg* is slower than preg*
  2. to match line-feeds and carriage returns, you need to use a double quote string.. or use the preg function modifier 's'
Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url]