检查字符串长度,最大值和最小值

时间:2011-03-31 19:29:52

标签: php strlen

是否有函数来检查字符串是太长还是太短,我通常最终在几个地方写这样的东西:

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.";
    }

如何将其变成函数^?

7 个答案:

答案 0 :(得分:6)

我猜你可以做这样的功能:

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;
}

然后你可以这样做:

$errors['field_name'] = validStrLen($field, 12, 20);

答案 1 :(得分:0)

如下:

$GLOBALS['errors'] = array();

function addError($msg) {
    $GLOBALS['errors'][] = $msg;
}

function printErrors() {
    foreach ($GLOBALS['errors'] as $err)
        echo "$err\n";
}

根据需要多次调用addError('error message here'),然后在最后调用printErrors()。

答案 2 :(得分:0)

您可以使用session vars制作错误功能:

//at the top of the file:
session_start();
//....code
error("Field Name is too short, minimum is 12 characters (20 max).");
//.. some code
//at the end of the file:
displayErrors();

function error($msg){
   if(!isset($_SESSION['errors'])) $_SESSION['errors'] = array();
   $_SESSION['errors'][] = $msg;
}

function displayErrors(){
     foreach($_SESSION['errors'] as $err){
         echo $err.'<br/>'.PHP_EOL;
     }
     unset($_SESSION['errors']);
}

在此演示: http://codepad.org/TKigVlCj

答案 3 :(得分:0)

/* Helper function */
function validateLength($value, $minLength, $maxLength, $fieldTitle) {
    $valueStrLen = strlen($value);

    if ($valueStrLen < $minLength) {
        return "$fieldTitle is too short, minimum is $minLength characters ($maxLength max).";
    } elseif($valueStrLen > $maxLength) {
        return "$fieldTitle is too long, maximum is $maxLength characters.";
    } else {
        return '';
    }   
}

/* Example of usage */

$errors = array();

if ( $err = validateLength($_GET['user_name'], 12, 20, 'User name') ) {
    $errros['user_name'] = $err;
}

if ( $err = validateLength($_GET['user_pass'], 12, 20, 'Password') ) {
    $errros['user_pass'] = $err;
}

答案 4 :(得分:0)

function validateLenght($s, $min, $max) {
    if (strlen($s) > $max) { return 2; }
    elseif (strlen($s) < $min) { return 1; }
    else { return 0; }
}

if (validateLenght('test', 5, 20) > 0) {
    echo 'Username must be between 5 and 20 characters.';
}

答案 5 :(得分:0)

PHP验证您可以使用的最小和最大整数:

$quantity = 2;
if (filter_var($quantity, FILTER_VALIDATE_INT, array("options" => array("min_range"=>1, "max_range"=>10))) === false) {
    echo("Quantity is not within the legal range");
} else {
    echo("Quantity is within the legal range");
}

答案 6 :(得分:-1)

这个怎么样:

if(((strlen($input)<12)||(strlen($input) > 20)))
    {
        $errors['field_name'] = "Must be 12-20 characters only. Please try again.";         
    }