Is there a function to check if a string is too long or too short, I normally end up writing something like this in several places:
if (strlen($input) < 12)
{
echo "Input is too short, minimum is 12 characters (20 max).";
}
elseif(strlen($input) > 20)
{
echo "Input is too long, maximum is 20 characters.";
}
I know you can easily write one but is there one built into PHP?
I normally collect errors as I validate input, so the above code would be written:
$errors = array();
if (strlen($input) < 12)
{
$errors['field_name'] = "Field Name is too short, minimum is 12 characters (20 max).";
}
elseif(strlen($input) > 20)
{
$errors['field_name'] = "Field Name is too long, maximum is 20 characters.";
}
How can that be made into a function ^?
I guess you can make a function like this:
function validStrLen($str, $min, $max){
$len = strlen($str);
if($len < $min){
return "Field Name is too short, minimum is $min characters ($max max)";
}
elseif($len > $max){
return "Field Name is too long, maximum is $max characters ($min min).";
}
return TRUE;
}
Then you can do something like this:
$errors['field_name'] = validStrLen($field, 12, 20);