I know this thread been pretty old but I just wanted to share one of my function. This function below can check for empty strings, string with maximum lengths, minimum lengths, or exact length. If you want to check for empty strings, just put $min_len and $max_len as 0.
function chk_str( $input, $min_len = null, $max_len = null ){
if ( !is_int($min_len) && $min_len !== null ) throw new Exception('chk_str(): $min_len must be an integer or a null value.');
if ( !is_int($max_len) && $max_len !== null ) throw new Exception('chk_str(): $max_len must be an integer or a null value.');
if ( $min_len !== null && $max_len !== null ){
if ( $min_len > $max_len ) throw new Exception('chk_str(): $min_len can\'t be larger than $max_len.');
}
if ( !is_string( $input ) ) {
return false;
} else {
$output = true;
}
if ( $min_len !== null ){
if ( strlen($input) < $min_len ) $output = false;
}
if ( $max_len !== null ){
if ( strlen($input) > $max_len ) $output = false;
}
return $output;
}