Working with Regular Expressions with Filters in PHP 5

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.

Thus, if you’re interested in learning how to put this group of filters to work for you in a very short time, begin reading this series of articles, since in it you’ll find the right pointers to get you started using the filter library in a true painless fashion.

However, if you already had the opportunity to read all of the preceding chapters of this series, it’s possible that you now have a pretty solid background in  utilizing this extension. In those tutorials I used several hands-on examples to explain how to validate integers and float numbers, and to check Boolean values by means of a couple of intuitive PHP functions and simple constants.

Now, speaking more specifically, in the previous article I explained how to use the “filter_var()” function in conjunction with the FILTER_VALIDATE_FLOAT filter to validate floating numbers assigned to a single scalar variable and stored in arrays, which speaks for itself about the versatility of the filter in question.

Nonetheless, this instructive journey on which we’re exploring the capabilities offered by the filter extension doesn’t stop there. In this fifth part of the series, I’m going to discuss the usage of the extension for checking if the value that has been assigned to a given variable follows a specified regular expression pattern.

That sounds pretty interesting, right? Therefore, if I’ve caught your attention, then it’s time to continue discovering many other handy validation features provided by the PHP 5 filter library. Let’s get going!

{mospagebreak title=Review: validating floating numbers with the filter extension}

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.

{mospagebreak title=Checking regular expressions with the filter extension}

It’s probable that during your life as PHP developer, you have used regular expressions on numerous occasions to validate well-formatted IP and email addresses, phone numbers, alphabetic data, and so forth, so I will assume that you’re familiar with its most basic aspects.

The filter extension provides another filter called FILTER_VALIDATE_REGEXP, which permits you to determine if the value stored in a variable matches a specified regular expression pattern. To demonstrate more clearly how this filter does its thing, below I included a new code sample that simply checks if a given string begins with the uppercase A character. Here it is:

// example on working with basic regular expressions

 

$value = ‘Alejandro Gervasio';

if(filter_var($value, FILTER_VALIDATE_REGEXP, array(‘options’ => array(‘regexp’ => "/^A(.+)/"))) === FALSE) // displays Match was found.

{

echo ‘No match was found.';

}

else

{

echo ‘Match was found.';

}

Here you have it. As shown above, the FILTER_VALIDATE_REGEXP filter comes in extremely handy for validating values according to a specific regular expression. Of course, in this particular case, I decided to keep the sample regular expression very simple, to help you grasp more quickly how this filter works; naturally, more complex expressions can be used to suit different requirements.

Below there’s a similar example that uses the FILTER_VALIDATE_REGEXP filter, but this time for determining if the string being evaluated starts with the “G” letter. Look at it, please:

// example on working with basic regular expressions

 

$value = ‘Alejandro Gervasio';

if(filter_var($value, FILTER_VALIDATE_REGEXP, array(‘options’ => array(‘regexp’ => "/^G(.+)/"))) === FALSE) // displays No match was found.

{

echo ‘No match was found';

}

else

{

echo ‘Match was found.';

}

Definitely, understanding the logic that drives the previous example is a pretty straightforward process that can be tackled with minor hassles. Again, it’s valid to note how the regular expression is passed to the “filter_var()” function as an additional argument, which we already saw in the preceding code sample.

Well, at this stage I provided you with a quick overview of how to use the FILTER_VALIDATE_REGEXP filter to determine if the value assigned to a variable matches a given regular expression pattern. Next I will explain how to utilize the functionality of the filter extension for validating URLs.

As you might have guessed, this new task will be performed by way of yet another filter. Thus, to learn more about it, click on the link that appears below and read the next section.

{mospagebreak title=Validating URLs at a basic level}

As I expressed in the previous section, the filter extension offers yet another handy filter that can be used for validating URLs in all sorts of clever ways. The filter utilized for performing this task is called simply FILTER_VALIDATE_URL, and one use of it has been illustrated below:

// validate URL

$url = ‘http://www.devshed.com';

if(filter_var($url, FILTER_VALIDATE_URL) === FALSE) // displays The URL provided is valid.

{

echo ‘The URL provided is not valid.';

}

else

{

echo ‘The URL provided is valid.';

}

In its simplest version, the FILTER_VALIDATE_URL filter will merely check to see if a given URL is correctly formatted. In the above example, the filter will consider the incoming URL to be valid, since it indeed is properly formed — but now pay attention to the following code sample, which passes to the filter only the URL’s protocol, and in consequence produces an error:

// validate URL

$url = ‘http://';

if(filter_var($url, FILTER_VALIDATE_URL) === FALSE) // displays The URL provided is not valid.

{

echo ‘The URL provided is not valid.';

}

else

{

echo ‘The URL provided is valid.';

}

Even though the example is rather simplistic and trivial, it shows in a nutshell how to work with the FILTER_VALIDATE_URL filter to validate well-formatted URLs. Of course, this filter comes packed with a few other useful options that allow you to check the host, path and protocol sections of URLs, but these will be discussed in detail in the next article. Meanwhile, feel free to edit all of the code samples developed in this article so you can arm yourself with a more solid foundation in using the filter PHP 5 extension.

Final thoughts

It’s hard to believe, but we’ve come to the end of this fifth chapter of the series. The experience has been quite instructive, since you’ve learned the basic concepts that surround the utilization of the filter extension for working with a few simple regular expressions, and for validating basic URLs at a very basic level.

And speaking of checking URLs, the filter library provides PHP programmers with the ability to validate them in a decent variety of flavors, including the corresponding protocol, the host and path and a few other really handy options that will be discussed in depth in the upcoming tutorial.

Therefore, now that you’ve been warned about the juicy topics that will be covered in the next tutorial, you don’t have any excuses to miss it!

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

antalya escort bayan antalya escort bayan Antalya escort diyarbakir escort