Re: regular exp if help
| From: | Hans H. Anderson | Date: | Wed, 23 Aug 2000 21:18:04 +0000 |
| Subject: | Re: regular exp if help | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-13249@lists.php.net to get a copy of this message | ||
On Wed, 23 Aug 2000, Chris Murley wrote:
> i tried this code:
>
> if ( !ereg( "([a-zA-Z0-9])" , $Username) ){
> exit;
> }
>
> it works when i set $Username="'" but if i use
> $Username="i'm cool"
>
> it says thats ok
That regex matches any single lower alpha, upper alpha or numeric
character. If you are trying to match if there are any characters that
are *not* a-zA-Z0-9, you should use the character class negation:
[^A-Za-z0-9]
That will match any character *not* A-Za-z0-9. So, it will match the
single quote in your first example like it should, and the single quote
in the second example, too, like it should. The first regex, without the
character negation, would match any A-Za-z0-9. Since I'm Cool has six of
those matches (I,m,C,o,o,l), it returned true.
Hans Anderson ~ me@hansanderson.com
PO Box 426, Missoula, MT 59806 | PHP, Search Engines, SQL
(800)397-9116 / (406)549-6524 | Databases, Flash Multimedia,
http://www.hansanderson.com | Killer Audio Production,
http://www.goofiness.com | Minty Fresh Breath.