Re: REG-EXPR: Allowing limited number of special chars in usernames
| From: | Barry | Date: | Wed, 15 Mar 2006 15:27:20 +0000 |
| Subject: | Re: REG-EXPR: Allowing limited number of special chars in usernames | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-231949@lists.php.net to get a copy of this message | ||
Holger Sunke wrote:
Hello, I dont know that much aboput regular expressions and just want to know how to find out the number of special (non alphanumeric) characters in a string or how to match a string that contains less than 3 special chars. urrently im using a function function valid_username($string) { if(strlen($string)<=16&&strlen($string)>5) {Greets Barry -- Smileys rule (cX.x)C --o(^_^o) Dance for me! ^(^_^)o (o^_^)o o(^_^)^ o(^_^o)return(preg_match('/^[ -~äöüßÄÖÜ]+$/',$string));} else {return FALSE;} } This allows usernames of 6 to 16 length containing many special chars and German "Umlaute". But this also allows usernames like "=)/(&%$" what should not be. So how to limit the number of special chars? This function should just return fals if number of special chars > X Greets, Holger Sunke One thing that comes to my mind would be splitting up the string into letters, numbers and signs with ereg()/preg() and count each one.