Sanitize and Validate a URL

If you have found the validation of all data coming from the user is rather heavy, you do not know how to validate or you think it is essential to use a regular expression to validate an address mail, you fell on the right article. The latter is suggested by a php developers and will be help you solve your biggest problem.

Filters in PHP

PHP, filters role is to enable you to ensure that all the data you have received is the right type. Ie that it is real and integer data. They will also be in good shape. The PHP is a weakly typed language. Therefore, it is best to thoroughly check the type of data your users will send you so that you will not have unpleasant surprises.

With filters, you can valier of multiple data types such as integers, boolean, real numbers and email addresses, URL and IP addresses. So you will not need to use regular expressions. However, filters are not only used to validate the data, they can also be used for cleaning of the latter.

What type of cleaning?

The filters also have the ability to cleanse data. For strings for example, they can remove the tags (x) HTML), a clean-mail address, a number or a URL. There are actually characters that are not allowed in email addresses.

The cleaning does not validate the data. It will only removes only the characters that you have not allowed even if there was no variable validity.

To validate a variable, use filter_var (). The latter is often called with two parameters, the variable to validate and filter type. However, it is also possible to use a third which is the array of options. It aims to return the value typecast if this is not the right type. The feature will allow FILTER_SANITIZE_URL meanwhile, clean the data as a link.

Discover our last paper