As I anticipated in the section that you just read, the filter extension allows you to validate URLs more strictly, by using a few additional arguments in conjunction with the FILTER_VALIDATE_URL filter. Of course, to illustrate more clearly how these arguments can be used in concrete cases, below I coded two brand new examples. They make use of the FILTER_FLAG_SCHEME_REQUIRED option for validating the correct scheme of a given URL. Look at them, please: (example on validating URL’s using the FILTER_FLAG_SCHEME_REQUIRED argument)
$url = 'index.php'; // validate URL if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) === FALSE) // displays The URL provided is not valid. { echo 'The URL provided is not valid.'; } else { echo 'The URL provided is valid.'; }
(example on validating URL’s using the FILTER_FLAG_ SCHEME_REQUIRED argument)
$url = 'http://devshed'; // validate URL if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) === FALSE) // displays The URL provided is valid. { echo 'The URL provided is not valid.'; } else { echo 'The URL provided is valid.'; } Hopefully, the examples shown above give you a clear idea of how to validate the schema of a URL by using the FILTER_FLAG_SCHEME_REQUIRED option, which has been passed as an additional argument to the “filter_var()” function. In the first case, the schema of the URL being checked is actually correct, so the script will display a successful message on screen. On the other hand, the last example will complain about the URL’s schema, since it’s simply incorrect. Now that you've learned how to validate the schema of a specified URL, let me show you another pair of examples which demonstrate how to determine if the URL in question contains a host portion. This time we'll use an entirely new argument called FILTER_FLAG_HOST_REQUIRED. Here are the aforementioned examples:
// example on validating URL’s using the FILTER_FLAG_HOST_REQUIRED argument
$url = 'http://devshed.com'; // validate URL if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) === FALSE) // displays The URL provided is valid. { echo 'The URL provided is not valid.'; } else { echo 'The URL provided is valid.'; }
// example on validating URL’s using the FILTER_FLAG_HOST_REQUIRED argument
$url = 'http://'; // validate URL if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) === FALSE) // displays The URL provided is not valid. { echo 'The URL provided is not valid.'; } else { echo 'The URL provided is valid.'; } Do you realize how simple it is to determine if a specified URL includes a host section? I guess you do. However, if you have any doubts about how this process is achieved, the above examples should help to dissipate them quickly. As you can see, in both cases the FILTER_FLAG_HOST_REQUIRED argument is passed to the “filter_var()” function to check if the host portion of the URL being validated is present or not. Of course, the first example will consider the incoming URL to be valid. The second case, on the other hand, will display a trivial error message on the browser, since the host hasn’t been specified. So far, so good, right? At this stage, I’m sure that you've already grasped the logic that stands behind validating specific sections of a URL by means of the FILTER_VALIDATE_URL filter and the “filter_var()” function. Therefore, the last thing that I’m going to discuss in this tutorial will consist of demonstrating how to use the filter to determine if a given URL contains a path portion. As you might have guessed, this task will be performed through another additional argument of the FILTER_VALIDATE_URL filter. But if you wish to learn the full details of this process, now click on the link below and read the following segment.
blog comments powered by Disqus |
|
|
|
|
|
|
|