Definitely, an appropriate epilogue for this article consists of showing how to use the FILTER_VALIDATE_URL filter for checking to see whether or not a given URL includes a path. Similar to the examples developed in the previous section, this validation process can be easily accomplished by means of another argument called FILTER_FLAG_PATH_REQUIRED, which must be passed to the “filter_var()” function that you learned before. Having said that, here’s a basic example that shows how to work with the argument when validating a simple URL. Have a look at it: // example on validating URLS using the FILTER_FLAG_PATH_REQUIRED argument
$url = 'http://devshed.com/path/to/images/'; // validate URL if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED) === FALSE) // displays The URL provided is valid. { echo 'The URL provided is not valid.'; } else { echo 'The URL provided is valid.'; }
As shown above, actually it’s pretty easy to determine if a URL contains a path portion by using the FILTER_FLAG_PATH_REQUIRED argument. In the example coded before, a fictional path has been specified for the first case, meaning that the “filter_var()” function will consider the inputted URL valid. Moving forward, take a look at the following example, which shows how to determine if a URL contains a query string by way of another argument called FILTER_FLAG_QUERY_REQUIRED. Here it is: // example on validating URLS using the FILTER_FLAG_QUERY_REQUIRED argument
$url = 'http://devshed.com/?id=1'; // validate URL if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED) === FALSE) // displays The URL provided is valid. { echo 'The URL provided is not valid.'; } else { echo 'The URL provided is valid.'; } Finally, with this concluding code sample, I’m finishing this introduction to working with the FILTER_VALIDATE_URL filter. When used in conjunction with a few other handy arguments, as you saw it permits you to validate different sections of a URL, such as its host, path, query strings and so forth. As always, feel free to edit all of the examples built in this tutorial, since doing so will help you to acquire a better background in working with the PHP 5 filter extension. Final thoughts Over the course of this sixth part of the series, I provided you with a quick overview of the different options available with the FILTER_VALIDATE_URL filter, which allows you to evaluate several sections of a specified URL and perform a more strict validation process on it. In the upcoming article, I’m going to continue exploring the powerful capabilities given by the filter PHP 5 extension, this time by showing how to utilize it for validating IP addresses and sanitizing strings as well. Thus, here’s my final suggestion: don’t miss the next article!
blog comments powered by Disqus |
|
|
|
|
|
|
|