开发者

Checking string length, max and minimum

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, ma开发者_运维知识库ximum 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);


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


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.


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


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


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


How about this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜