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 thoughtsIt’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!
blog comments powered by Disqus |
|
|
|
|
|
|
|