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

Checking regular expressions 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

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.



 
 
>>> 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: