I have a small function which I regularly use to check if a variable contains only [a-z][0-9] and the special chars '-' and '_'. Currently I'm using the following:
function is_clean($string){
$pattern = "/([a-z]|[A-Z]|[0-9]|-|_)*/";
preg_match($pattern, $string, $return);
$pass = (($string == $return[0]) ? TRUE : FALSE);
return $pass;
}
1 - Can anyone tell me if this is the best/most efficient way to do this and if not try to explain why?
2 - When I view this function through my IDE I get a warning that $return is uninitialized... should I be initializing variables in advance with php, if so how and why?