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

Validating URLs at a basic level - 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

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!



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