Data filters first appeared in PHP 5.0, and by some coincidence, most of the coders went unnoticed. Perhaps this can be explained by the absence of something like this in PHP4, or maybe the manual was just poorly read. I also learned about them by chance ... But this wonderful function allows you to get rid of confusing, and sometimes incorrect, regular expressions, when performing typical tasks.
The manual on filters is
here . I will give only basic information and a couple of practical examples.
Closer to the point
First, the filters are divided into 3 types:
- Validate Filters - Test Filters
- Sanitize Filters - de-filtering filters
- Other Filters - Other Filters
Filters of the first type check if the string matches the filter. The response from such a filter is the initial string in case of success or
false .
Filters of the second type process the string and return it in filtered form.
The third type includes only one filter -
FILTER_CALLBACK , which will pass the string to the user-defined function and return its response.
The main function for working with filters is
filter_var :
mixed filter_var ( mixed $variable [, int $filter = FILTER_DEFAULT [, mixed $options ]] )
The first parameter is the variable to be validated, the second is the filter number, which is conveniently written with a predefined constant. The third parameter can be passed additional options, as a rule - special filter flags. The return value depends on the filter.
Lastly, a couple of examples of work: