note 92393 modified in function.ctype-alnum by danbrown

From: Date: Wed, 22 Jul 2009 21:48:04 +0000
Subject: note 92393 modified in function.ctype-alnum by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-158596@lists.php.net to get a copy of this message
ctype_alnum() is a godsend for quick and easy username/data filtering when used in conjunction with str_replace(). Let's say your usernames have dash(-) and underscore(_) allowable and alphanumeric digits as well. Instead of a regex you can trade a bit of performance for simplicity: <?php $sUser = 'my_username01'; $aValid = array('-', '_'); if(!ctype_alnum(str_replace($aValid), '', $sUser)) { echo 'Your username is not properly formatted.'; } ?> --was-- ctype_alnum() is a godsend for quick and easy username/data filtering when used in conjunction with str_replace(). Let's say your usernames have dash(-) and underscore(_) allowable and alphanumeric digits as well. Instead of a regex you can trade a bit of performance for simplicity: <? $sUser = 'my_username01'; $aValid = array('-', '_'); if(!ctype_alnum(str_replace($aValid), '', $sUser)) { echo 'Your username is not properly formatted.'; } ?> http://php.net/manual/en/function.ctype-alnum.php

« previous php.notes (#158596) next »