是否有检查字符串是否太长或太短的功能,我通常最终会在几个地方写这样的东西:
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.";
}
我知道您可以轻松编写一个,但是 PHP 中是否内置了一个?
我通常在验证输入时收集错误,因此将编写上述代码:
$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.";
}
怎么能把它变成一个函数^?