Re: Regular expression problems

From: Date: Mon, 26 Feb 2001 03:45:42 +0000
Subject: Re: Regular expression problems
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-41641@lists.php.net to get a copy of this message
From: "Dan Watt" <danwatt@mediaone.net> > Im trying to build a user login system, and when there is a new user, I need > to validate that they are usign using word characters ([0-9A-Za-z_] or > \w)... I have TRIED MANY different regular expressions to test this, but > none work. This seems so simple but I am missing something. I need a > expression that returs true ONLY if ALL the characters match \w .... Any > help would be appreciated. > > <?php $username = "johnny"; if (!eregi("[^A-Z0-9_-]", $username)) { echo "username OK"; } else { echo "bad username!"; } ?> Assuming you only want letters, numbers, underscores or hyphens. If you really want \w you would need to use preg instead of ereg, and then you need PCRE support in PHP. Or you could use ereg's character classes, I think the pattern would look like: "[^[:alphanum:]]". However you are probably better off using an explicit set of characters as I have shown, so that it is obvious what is and is not allowed. Cheers Simon Garner

« previous php.general (#41641) next »