Checking string length, max and minimum

我的梦境 提交于 2019-12-08 03:40:22

问题


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 ^?


回答1:


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



回答2:


How about something like:

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

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

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

Just call addError('error message here') as many times as you need, followed by a printErrors() call at the end.




回答3:


you can make a error function that utilizes 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']);
}

Demo here: http://codepad.org/TKigVlCj




回答4:


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



回答5:


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



回答6:


PHP validate minimum and maximum integer number you can use this:

$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");
}


来源:https://stackoverflow.com/questions/5505227/checking-string-length-max-and-minimum

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!