This utility takes the input and verifies that it's length is
between the minimum number of characters and maximum number
of characters. A length precisely equal to the minimum
number of characters or precisely equal to the maximum number
of characters will be considered valid and not manipulated.
When would you use this? The best example is a script
that is requesting the user select a User Name between x and xx
characters in length. This utility will ensure that the name
input is exactly between those values. It is different than
using the substring because the substring won't default on a
minimum number of characters and you would have to write the
length checking and length testing statements and conditionals
and an error would cause a fault. Using this utility takes all
that checking away and worry away. It simply returns a value
that is within the specified parameters.
To call the script you pass it three variables. The input to
be checked, the minimum number of characters and the maximum
number of characters. Here are a few examples of calls to the
program showing the input, the call and the output.
- EX.1
-
$input = "jimbob";
$output = format_length($input, 5, 10);
$output will print out "jimbob";
- EX.2
-
$input = "jimbob";
$output = format_length($input, 2, 4);
$output will print out "jimb";
- EX.3
-
$input = "jimbob";
$output = format_length($input, 10, 15);
$output will print out "jimbobjimbob";
- EX.4
-
$input = "jb";
$output = format_length($input, 5, 10);
$output will print out "jbjbjbjb";
NOTE: If you do not specify a minimum and or maximum number
of characters, the utilities default setting is the number
"6" for minimum and "10" for maximum.
|