adam84 Posted August 10, 2008 Share Posted August 10, 2008 I want to accept only letters, periods, single quote and spaces. If I take out the \s, the expression works fine, but once I add the \s for the space in, it never passes. Any Ideas on what I should do? This is what I have: if( !eregi("^[A-Za-z.'\s]+$", $city) ) { echo "<BR><FONT COLOR=RED><B>Invalid city</B></FONT>"; return false; } Quote Link to comment https://forums.phpfreaks.com/topic/119058-solved-eregi-help/ Share on other sites More sharing options...
nrg_alpha Posted August 11, 2008 Share Posted August 11, 2008 I want to accept only letters, periods, single quote and spaces. If I take out the \s, the expression works fine, but once I add the \s for the space in, it never passes. Any Ideas on what I should do? This is what I have: if( !eregi("^[A-Za-z.'\s]+$", $city) ) { echo "<BR><FONT COLOR=RED><B>Invalid city</B></FONT>"; return false; } For starters, I would avoid using ereg and start using preg (which is part of PCRE - or Perl Compatible Regular Expressions). As of PHP 6, support for POSIX (which uses the ereg syntax) will be dropped. PCRE is more robust and powerful. Here is what you were trying to do , but using preg: $city = 'New York'; if(preg_match('#^[a-zA-Z\s\.\']+$#', $city)){ echo $city . ' meets the criteria!'; } else { echo $city . ' does not meet the criteria!'; } Notice how I used the escape character for the dot (which by itself without escape character is basically a wildcard that can be any character [execpt for \r, \n or \t if I am not mistaken]). Since I am using single quotes, I have to escape the single quote inside the delimiters so that the system knows I am actually referring to a literal single quote as opposed to confusing the system with a single quote (which closes the pattern which PCRE matches against). So with or without a space, this work. Cheers, NRG Quote Link to comment https://forums.phpfreaks.com/topic/119058-solved-eregi-help/#findComment-613194 Share on other sites More sharing options...
effigy Posted August 11, 2008 Share Posted August 11, 2008 Notice how I used the escape character for the dot (which by itself without escape character is basically a wildcard that can be any character [execpt for \r, \n or \t if I am not mistaken]). Metacharacters change within character classes--[.] will only match a period. I want to accept only letters, periods, single quote and spaces. If you only want spaces, be wary of \s. It encompasses all whitespace, thus allowing tabs, new lines, etc. Quote Link to comment https://forums.phpfreaks.com/topic/119058-solved-eregi-help/#findComment-613562 Share on other sites More sharing options...
adam84 Posted August 11, 2008 Author Share Posted August 11, 2008 If you only want spaces, be wary of \s. It encompasses all whitespace, thus allowing tabs, new lines, etc. So since I am wary, what would I using instead of \s? Quote Link to comment https://forums.phpfreaks.com/topic/119058-solved-eregi-help/#findComment-613570 Share on other sites More sharing options...
effigy Posted August 11, 2008 Share Posted August 11, 2008 A space: [A-Za-z.' ]+ Quote Link to comment https://forums.phpfreaks.com/topic/119058-solved-eregi-help/#findComment-613590 Share on other sites More sharing options...
nrg_alpha Posted August 11, 2008 Share Posted August 11, 2008 Notice how I used the escape character for the dot (which by itself without escape character is basically a wildcard that can be any character [execpt for \r, \n or \t if I am not mistaken]). Metacharacters change within character classes--[.] will only match a period. Oops.. I stand corrected (I'm so used to needing to escape the dot). My bad Cheers, NRG Quote Link to comment https://forums.phpfreaks.com/topic/119058-solved-eregi-help/#findComment-613699 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.