3

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

john mossel
  • 2,158
  • 5
  • 24
  • 39
  • What would you like function to accept (i.e. its parameters) and return? There is no function in PHP to *exactly* replicate your code snippet but we can help you to write one. – salathe Mar 31 '11 at 19:34
  • I've written one that builds up a list of errors: function check_string_length($string, $label, $min, $max), but I though there has to be a more elegant way of doing this. – john mossel Mar 31 '11 at 19:48

7 Answers7

9

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);
joanis
  • 10,635
  • 14
  • 30
  • 40
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
1

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");
}
Muhammad Shahzad
  • 9,340
  • 21
  • 86
  • 130
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.';
}
Brynner Ferreira
  • 1,527
  • 1
  • 21
  • 21
0

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.

Unsigned
  • 9,640
  • 4
  • 43
  • 72
0

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

Naftali
  • 144,921
  • 39
  • 244
  • 303
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;
}
Pixus.ru
  • 106
  • 3
-1

How about this:

if(((strlen($input)<12)||(strlen($input) > 20)))
    {
        $errors['field_name'] = "Must be 12-20 characters only. Please try again.";         
    }
Vem
  • 3
  • 3
  • 1
    The post asked for a function to check the length of the string. He specifically asked for a function, not an if statement. – Linek Mar 24 '21 at 15:11