note 51977 deleted from function.preg-split by betz
| From: | betz@php.net | Date: | Thu, 21 Apr 2005 10:50:06 +0000 |
| Subject: | note 51977 deleted from function.preg-split by betz | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-88327@lists.php.net to get a copy of this message | ||
Note Submitter: thejahn1 at yahoo dot com
----
Here is a way to make sure the user only has valid characters in a string... Useful for validating
usernames...
<?php
function blow_up($str){
return preg_split("//", $str, -1, PREG_SPLIT_NO_EMPTY);
}
$userstr= "the jahn!";
$user= blow_up(strtolower($userstr));
$valchars= "abcdefghijklmnopqrstuvwxyz0123456789";
$valchars= blow_up($valchars);
$inval= '';
for($i=0;$i<count($user);$i++){
if(!in_array($user[$i], $valchars)){
$inval= $inval.$user[$i];
}
}
if(strlen($inval)>0){
echo 'System found '.strlen($inval).' invalid characters.<br>';
echo $inval;
}else{
echo 'No invalid characters detected...';
}
?>