Home arrow PHP arrow Page 2 - Working with Regular Expressions with Filters in PHP 5

Review: validating floating numbers with the filter extension - PHP

Among the enhancements that were introduced originally in PHP 5, thereís one powerful extension that has been all but ignored by many programmers until now, quite possibly because they werenít aware of its existence. Yes, as you might have guessed, Iím talking about the filter library -- a handy set of filters and functions that allow you to validate different types of data with remarkable ease.

TABLE OF CONTENTS:
  1. Working with Regular Expressions with Filters in PHP 5
  2. Review: validating floating numbers with the filter extension
  3. Checking regular expressions with the filter extension
  4. Validating URLs at a basic level
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
August 12, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In case you still havenít read the previous installment of this series, where I discussed the usage of the FILTER_VALIDATE_FLOAT filter for checking float numbers, hereís the group of code samples I developed in that article. They demonstrate how to work with it, in conjunction with the already familiar ďfilter_var()Ē function. Take a look at them, please:

(basic example on validating float numbers)

 

$floatValue = 1.23456;

if(filter_var($floatValue, FILTER_VALIDATE_FLOAT) === FALSE) // displays Input is a valid float number.

{

echo 'Input is not a valid float number.';

}

else

{

echo 'Input is a valid float number.';

}

 

 

(example on validating float numbers using an array of values)

 

$values= array(10.75, '10.75', -1234.5678, 'This is a string', array());

$filteredValues = filter_var($values, FILTER_VALIDATE_FLOAT, FILTER_REQUIRE_ARRAY);

var_dump($filteredValues);

 

/*

displays the following

array

0 => float 10.75

1 => float 10.75

2 => float -1234.5678

3 => boolean false

4 =>

array

empty

*/

 

 

(example on validating float numbers using a separator)

 

$values= array(10.75 => '.', '10,75' => ',');

foreach ($values as $value => $separator)

{

$filteredValue = filter_var($value, FILTER_VALIDATE_FLOAT, array('options' => array('decimal' => $separator)));

var_dump($filteredValue);

}

 

/* displays the following

float 10

 

float 10.75

*/  

As depicted by the above examples, the filter extension makes it really easy to validate float numbers using the FILTER_VALIDATE_FLOAT filter. As you can see, the first case shows how to use the filter to check a single float number, while the second code sample is a bit more complex, since it demonstrates how to check floating numbers stored in a trivial array.

Finally, the last example is perhaps the most useful of all, because it illustrates the usage of the ďoptionsĒ argument. This arguments permits you to specify which decimal delimiter will be utilized when validating a given float number. That was really simple to grasp, wasnít it?

So far, everything should look pretty good to you; you've hopefully recalled how to work with the FILTER_VALIDATE_FLOAT filter in different cases. Considering this, itís time to explore other useful validation capabilities given by the PHP 5 filter extension.

So, in accordance with the concepts I deployed in the introduction, in the course of the following segment Iím going to explain how to use another filter that comes with this extension. As you'll see in a few moments, it's useful for determining if the value assigned to a variable follows a pattern defined through a regular expression.

This new topic will be covered in detail in the next section. So, to learn more about it, please click on the link shown below and keep reading.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: