PHP
  Home arrow PHP arrow Page 3 - Working with Regular Expressions with Filters in PHP 5
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
iPad news and developer info.
Ads by affinity 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
VPS Hosting 
Forums Sitemap 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Working with Regular Expressions with Filters in PHP 5


By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 1
    2009-08-12


    Table of Contents:
  • Working with Regular Expressions with Filters in PHP 5
  • Review: validating floating numbers with the filter extension
  • Checking regular expressions with the filter extension
  • Validating URLs at a basic level

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


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

    Working with Regular Expressions with Filters in PHP 5 - Checking regular expressions with the filter extension
    (Page 3 of 4 )

    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
     

       

    PHP ARTICLES

    - MVC and the Zend Framework
    - PHP: Rendering Web Pages Using the Composite...
    - PHP: Creating Dynamic Web Pages with the Com...
    - Secure Encrypting and Decrypting for Your PH...
    - PHP: Creating Single View Objects with the C...
    - PHP: View Object Collections and the Composi...
    - Securing Your PHP Website
    - Determine Link Relevance and Unique Class C ...
    - PHP Composite View Design Pattern: Introduci...
    - Facebook PHP API Applications: A Second Look
    - Secure PHP Programming
    - Facebook PHP API Applications: Basic Introdu...
    - SOAP Servers and Web Services
    - Using XPath, SOAP, and More with Web Services
    - Web Services: SimpleXML


    Code Analysis Tools
    Enterprise code analysis tools that deliver quality and reliable code



    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 12 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek