phpconstantssanitizationfilter-input

PHP - How to check if an input filter is valid?


I've got a function that takes a filter constant for filter_input as an optional parameter. Is it possible to make sure the value of the string is a valid PHP filter constant, perhaps with a built in PHP function?

I know one way is to make my own array of all filter constants and check against that, but I was hoping for an easier or better way.


Solution

  • You could verify against the list of filters:

    $valid = in_array($filter, filter_list(), true);
    

    Where $filter contains the user supplied filter value and $valid the result as a bool (true if valid, false if invalid).

    See filter_list() in the PHP manual for more details.