Validating Octal and Hexadecimal Values with Filters in PHP 5

As you know, when it comes to validating incoming data, PHP 5 comes bundled with a powerful set of native functions that allow you to perform all sorts of clever validations on a given variable. This includes checking for numeric values, arrays, strings, and objects as well. However, the best feature of the helpful validation capabilities offered by default by PHP 5 is its handy filter extension, even though it has been overlooked by many programmers so far.

Indeed, this library of filters also permits you to perform powerful validation on variables in a pretty straightforward fashion, without the need to define custom and complex functions. So, if you’re interested in learning how to use these filters within your own web applications, then this group of articles might be the material that you’re looking for.

Logically, if you already had the chance to read the introductory installment of this series, then at this point you’ll have a fairly clear idea of how to use the filter extension for validating integers. In that specific tutorial I coded a few basic examples that demonstrated how to carry out this task by using the FILTER_VALIDATE_INT filter in conjunction with the “filter_var()” function.

Not only did this filter allow us to check whether a given value was an integer or not, but it permitted us to determine if the value in question was within a specified range. This extra functionality was implemented via an additional “option” array, which was passed to the aforementioned “filter_var()” function.

However, I’m only scratching the surface when it comes to exploring the numerous capabilities offered by the filter extension. Therefore, in this second chapter of this series, I’m going to discuss how to use the FILTER_VALIDATE_INT filter that you learned in the previous part, this time for validating array elements as well as octal and hexadecimal integers.

Does this sound complex to you? It won’t be at all, trust me. Now, let’s get rid of the preliminaries and continue exploring the useful validation capacities of the filter PHP 5 extension!

{mospagebreak title=Review: the FILTER_VALIDATE_INT filter}

In case you still haven’t had an opportunity to read the preceding part of the series, where I discussed the use of the FILTER_VALIDATE_INT filter to validate integers, below I included the examples that I coded then, so you can examine them in detail. Here they are:

(example on validating an integer value)

 

$input = 123456;

// check to see if $input is an integer

echo filter_var($input, FILTER_VALIDATE_INT); // displays 123456

 

 

(example on validating a float value)

 

$input = 1.2;

// check to see if $input is an integer

echo filter_var($input, FILTER_VALIDATE_INT); // displays nothing

 

 

(example on validating a string value)

 

$input = ‘4.56’;

if(filter_var($input, FILTER_VALIDATE_INT) === FALSE) // displays Input is not an integer.

{

echo ‘Input is not an integer.';

}

else

{

echo ‘Input is an integer.';

}

 

 

(example on validating an integer value)

 

$input = ‘123456’;

if(filter_var($input, FILTER_VALIDATE_INT) === FALSE) // displays Input is an integer.

{

echo ‘Input is not an integer.';

}

else

{

echo ‘Input is an integer.';

}

 

 

(example on validating an integer value specifying min and max limits)

 

$min = 1;

$max = 99;

$input = 101;

if(filter_var($input, FILTER_VALIDATE_INT, array("options" => array("min_range" => $min, "max_range"=> $max ))) === FALSE) // displays Error: Input must be a value between 1 and 99 (Correct specification for the min and max options)

{

echo ‘Error: Input must be a value between 1 and 99.';

}

else

{

echo ‘Input is correct';

}

 

 

(example on validating an integer value specifying only max limit)

 

$min = 1;

$max = 99;

$input = 101;

if(filter_var($input, FILTER_VALIDATE_INT, array("options" => array("max_range"=> $max ))) === FALSE) // displays Error: Input must be a value between 1 and 99 (Specifies only the max option)

{

echo ‘Error: Input must be a value between 1 and 99.';

}

else

{

echo ‘Input is correct';

}

From the code samples listed above, it’s clear to see how easy it is to check whether or not the value assigned to a determined variable is an integer by using the FILTER_VALIDATE_INT filter in conjunction with the “filter_var()” function. In the two last examples, the filter in question has been used along with an additional “option” array, which allowed us to specify minimal and maximal boundaries for the integer being validated.

Now that you hopefully grasped the logic that stands behind using the FILTER_VALIDATE_INT filter, it’s time to continue exploring the functionality given by the filter extension. In the following section I’m going to explain how to use the FILTER_VALIDATE_INT filter for checking whether or not all the elements of an array are integers.

To see how this functionality of the FILTER_VALIDATE_INT filter will be used in a concrete example, please click on the link that appears below and keep reading.

{mospagebreak title=Filtering array elements} 

Quite possibly one of the most powerful functions that comes bundled with the filter extension is the one called “filter_var_array().” As its name implies, it can be used to apply, in one single round, a specified filter to all of the elements of an array.

To demonstrate the actual functionality of this function, I’m going to code a basic example that will use the FILTER_VALIDATE_INT filter that you saw in the previous section. Nonetheless, it’s valid to clarify that all the filters available with the extension can be used with the function in question, so you can imagine how useful it can be for checking multiple values. But it’s time to move past the theory and see how the function can be used to validate whether the elements of a given array are integers. The code sample below does that in a simple manner. Look at it:

<?php

 

// example on using the filter_var_array() function with FILTER_VALIDATE_INT filter

$values = array(1, ‘0.675’, -123456, ‘This is a string’, "abcdef", array(1, 2, 3));

// create an array of filtered values

$filtered_values = filter_var_array($values, FILTER_VALIDATE_INT);

foreach($filtered_values as $key=>$value)

{

echo ‘<p>’ . $key .’ —- ‘ . $value . ‘</p>';

}

 

/* displays the following

0 —- 1

 

1 —-

 

2 —- -123456

 

3 —-

 

4 —-

 

5 —- Array

*/

?>

Here you have it. As I mentioned before, the “filter_var_array()” function allows you to apply a specific filter to all of the elements of an array. In this particular case, I decided to use this function with the FILTER_VALIDATE_INT filter, but you may want to try using others and see how well it works.

Now, returning to the above example, you can see that the function also returns an array of mixed values, depending on the type of filter applied to the incoming data. Since in this particular case, I’m checking only for integers, the function displays nothing when this condition isn’t satisfied.

So far, so good. At this point I’m sure that you’ve already realized the potential of using the “filter_var_array()” function, meaning that it’s time to explore even more capabilities of the filter extension. The next thing that I’m going to discuss will consist of explaining how to utilize the FILTER_VALIDATE_INT filter, this time for validating both octal and hexadecimal integers.

Thus, if you wish to learn how this will be accomplished, click on the link below and read the next few lines.

{mospagebreak title=Validating octal and hexadecimal integers using the flags option}

One handy bonus given by the FILTER_VALIDATE_INT filter is its ability to check integers represented by numeration systems other than the decimal one. This extra functionality can be obtained by specifying a “flags” option when using the “filter_var()” previously explained. By means of these flags, it is possible, for instance, to validate both octal and hexadecimal integers with extreme ease.

The following two examples demonstrate a simple usage of the flags. Look at them, please:

<?php

 

// example on validating hexadecimal integers

// check hexadecimal integers

$hexValue = ‘0xf0′;

echo filter_var($hexValue, FILTER_VALIDATE_INT, array(‘flags’ => FILTER_FLAG_ALLOW_HEX)); // displays 240

 

 

// example on validating octal integers

$octValue = ‘7601’;

echo filter_var($octValue, FILTER_VALIDATE_INT, array(‘flags’ => FILTER_FLAG_ALLOW_OCTAL)); // displays 7601

?>

As I mentioned before, the “flags” option permits you to check for integer values that have been represented in octal and hexadecimal. In the first case, to validate a hexadecimal value, the FILTER_FLAG_ALLOW_HEX constant has been specified within the “filter_var()” function, while in the second example, the FILTER_FLAG_ALLOW_OCTAL constant has been passed to the function to check an octal integer.

And finally, with these last couple of code samples, I’m finishing this second part of the series that introduces the filter extension that comes included with PHP 5. As usual, feel free to edit all of the examples shown in this tutorial, so you can get a better idea of the numerous validation capabilities offered by this extension.

Final thoughts 

It’s hard to believe, but we’ve come to the end of this second part of this series. Hopefully the experience has been pretty educational, since you learned how to use the FILTER_VALIDATE_INT filter to validate arrays of values, as well as octal and hexadecimal integer values by using the “flag” option.

In the upcoming article, things will become even more interesting. I’m going to explain how to utilize the filter extension to check whether or not a value assigned to a variable is Boolean. So, now that you’ve been warned about the topics that will be covered in the next tutorial, you don’t have any excuse to miss it!

[gp-comments width="770" linklove="off" ]

chat