Re: REG-EXPR: Allowing limited number of special chars in usernames
| From: | Rafael | Date: | Thu, 16 Mar 2006 06:39:28 +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-232000@lists.php.net to get a copy of this message | ||
Holger Sunke wrote:
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.I don't really understand what you need, but... - how to find out the number of special (non alphanumeric) characters
$alpha = '[a-z0-9]';$special_chars = preg_replace("/$alpha+/Xi", '', $string); * this would replace any alpha-numeric char by... nothing, leaving
the "special chars" only, and strlen() will give you how many are* beware that you're saying *anything* other than alpha-numeric chars - how to match a string that contains less than 3 special chars * as I said above, strlen() will give you the number of these chars: $num_special_chars = strlen($special_chars);
preg_match('/^[ -~äöüßÄÖÜ]+$/',$string)will return TRUE if $string consists enterily of the chars between the square-brackets. I hope this helped a little.
urrently im using a function function valid_username($string) { if(strlen($string)<=16&&strlen($string)>5) {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 -- Atentamente, J. Rafael Salazar Magaña Innox - Innovación Inteligente Tel: +52 (33) 3615 5348 ext. 205 / 01 800 2-SOFTWARE http://www.innox.com.mx