Skip to main content

Module filter

Module filter 

Source
Expand description

filter_var(): the validation filters (FILTER_VALIDATE_INT, BOOL, FLOAT, REGEXP, URL, EMAIL, IP, MAC, DOMAIN), the sanitizing filters (FILTER_DEFAULT/FILTER_UNSAFE_RAW, FILTER_SANITIZE_*) and FILTER_CALLBACK, with their flags and options, ported from ext/filter (logical_filters.c, sanitizing_filters.c, callback_filter.c, filter.c) of PHP 8.5.

PHPRust
filter_var($value, $filter, $options)filter_var
filter_var($value, FILTER_CALLBACK, ['options' => $callable])filter_var_with

PHP values are Values (strings are bytes). The third argument is either flags or an options array (Options), read the way php_filter_call() reads it: "filter", "flags" and "options" keys, with zval_get_long()/zval_get_double() conversions of min_range and max_range, and the "default" option replacing a false (or, with FILTER_NULL_ON_FAILURE, null) result, even a valid false from FILTER_VALIDATE_BOOL.

FILTER_VALIDATE_REGEXP runs its regular expression on crate::pcre, through PHP’s compiled-pattern cache like PHP does. FILTER_VALIDATE_EMAIL checks the language of PHP’s fixed expressions directly (filter/email.rs), and runs the expression itself only for the addresses where PCRE2’s backtracking limit could decide the result. FILTER_VALIDATE_URL parses with crate::url::parse_url, or with crate::url::rfc3986 for "uri_parser_class" => Uri\Rfc3986\Uri.

FILTER_CALLBACK calls a PHP callable; filter_var_with takes the Callables that stand for them: whether the "options" entry is callable (PHP throws TypeError when it is not), and calling it with the value converted to a string. filter_var knows no callable.

Like PHP, filtering an array goes on after an element throws; the exception reported is the last one thrown (a failed validation does not throw while another exception is pending, and callables are not called).

FILTER_SANITIZE_FULL_SPECIAL_CHARS encodes as htmlentities() with the default charset, which this port fixes at PHP’s default, UTF-8 (default_charset/internal_encoding are not consulted).

Not supported (an Error with class Unsupported): the WHATWG URL parser (Uri\WhatWg\Url) for FILTER_VALIDATE_URL.

Re-exports§

pub use crate::pcre::Key;

Structs§

Error
An exception filter_var() throws (ValueError, TypeError, Filter\FilterFailedException), or Unsupported for what this port does not implement.
Filtered
What filter_var() returned, and the warning it emitted, if any.
NoCallables
No callable at all: FILTER_CALLBACK always throws TypeError.

Enums§

Options
The third argument of filter_var().
Value
A PHP value given to or returned by filter_var().

Constants§

FILTER_CALLBACK
Calls the callable in the options entry, as PHP’s FILTER_CALLBACK.
FILTER_DEFAULT
The default filter, which leaves the string as is, as PHP’s FILTER_DEFAULT.
FILTER_FLAG_ALLOW_FRACTION
Keeps the decimal separator when sanitizing floats, as PHP’s FILTER_FLAG_ALLOW_FRACTION.
FILTER_FLAG_ALLOW_HEX
Accepts hexadecimal integers (0x prefix), as PHP’s FILTER_FLAG_ALLOW_HEX.
FILTER_FLAG_ALLOW_OCTAL
Accepts octal integers (0 or 0o prefix), as PHP’s FILTER_FLAG_ALLOW_OCTAL.
FILTER_FLAG_ALLOW_SCIENTIFIC
Keeps e and E when sanitizing floats, as PHP’s FILTER_FLAG_ALLOW_SCIENTIFIC.
FILTER_FLAG_ALLOW_THOUSAND
Accepts or keeps thousand separators, as PHP’s FILTER_FLAG_ALLOW_THOUSAND.
FILTER_FLAG_EMAIL_UNICODE
Accepts Unicode in an email’s local part, as PHP’s FILTER_FLAG_EMAIL_UNICODE.
FILTER_FLAG_EMPTY_STRING_NULL
Turns an empty string into null, as PHP’s FILTER_FLAG_EMPTY_STRING_NULL.
FILTER_FLAG_ENCODE_AMP
Encodes & as &, as PHP’s FILTER_FLAG_ENCODE_AMP.
FILTER_FLAG_ENCODE_HIGH
Encodes bytes 127 and above as &#N;, as PHP’s FILTER_FLAG_ENCODE_HIGH.
FILTER_FLAG_ENCODE_LOW
Encodes bytes below 32 as &#N;, as PHP’s FILTER_FLAG_ENCODE_LOW.
FILTER_FLAG_GLOBAL_RANGE
Accepts globally routable IP addresses only, as PHP’s FILTER_FLAG_GLOBAL_RANGE.
FILTER_FLAG_HOSTNAME
Requires a valid host name for FILTER_VALIDATE_DOMAIN, as PHP’s FILTER_FLAG_HOSTNAME.
FILTER_FLAG_IPV4
Accepts IPv4 addresses only, as PHP’s FILTER_FLAG_IPV4.
FILTER_FLAG_IPV6
Accepts IPv6 addresses only, as PHP’s FILTER_FLAG_IPV6.
FILTER_FLAG_NONE
No flags, as PHP’s FILTER_FLAG_NONE.
FILTER_FLAG_NO_ENCODE_QUOTES
Leaves ' and " unencoded, as PHP’s FILTER_FLAG_NO_ENCODE_QUOTES.
FILTER_FLAG_NO_PRIV_RANGE
Rejects private IP ranges, as PHP’s FILTER_FLAG_NO_PRIV_RANGE.
FILTER_FLAG_NO_RES_RANGE
Rejects reserved IP ranges, as PHP’s FILTER_FLAG_NO_RES_RANGE.
FILTER_FLAG_PATH_REQUIRED
Requires a path in a validated URL, as PHP’s FILTER_FLAG_PATH_REQUIRED.
FILTER_FLAG_QUERY_REQUIRED
Requires a query string in a validated URL, as PHP’s FILTER_FLAG_QUERY_REQUIRED.
FILTER_FLAG_STRIP_BACKTICK
Strips backticks, as PHP’s FILTER_FLAG_STRIP_BACKTICK.
FILTER_FLAG_STRIP_HIGH
Strips bytes 127 and above, as PHP’s FILTER_FLAG_STRIP_HIGH.
FILTER_FLAG_STRIP_LOW
Strips bytes below 32, as PHP’s FILTER_FLAG_STRIP_LOW.
FILTER_FORCE_ARRAY
Wraps a scalar result in an array, as PHP’s FILTER_FORCE_ARRAY.
FILTER_NULL_ON_FAILURE
Returns null instead of false on failure, as PHP’s FILTER_NULL_ON_FAILURE.
FILTER_REQUIRE_ARRAY
Requires an array and filters each element, as PHP’s FILTER_REQUIRE_ARRAY.
FILTER_REQUIRE_SCALAR
Requires a scalar value, as PHP’s FILTER_REQUIRE_SCALAR.
FILTER_SANITIZE_ADD_SLASHES
Escapes as addslashes(), as PHP’s FILTER_SANITIZE_ADD_SLASHES.
FILTER_SANITIZE_EMAIL
Removes the bytes an email address cannot hold, as PHP’s FILTER_SANITIZE_EMAIL.
FILTER_SANITIZE_ENCODED
URL-encodes the string, as PHP’s FILTER_SANITIZE_ENCODED.
FILTER_SANITIZE_FULL_SPECIAL_CHARS
Encodes as htmlentities() with ENT_QUOTES, as PHP’s FILTER_SANITIZE_FULL_SPECIAL_CHARS.
FILTER_SANITIZE_NUMBER_FLOAT
Keeps only digits, signs and the allowed float characters, as PHP’s FILTER_SANITIZE_NUMBER_FLOAT.
FILTER_SANITIZE_NUMBER_INT
Keeps only digits and signs, as PHP’s FILTER_SANITIZE_NUMBER_INT.
FILTER_SANITIZE_SPECIAL_CHARS
HTML-encodes '"<>& and bytes below 32, as PHP’s FILTER_SANITIZE_SPECIAL_CHARS.
FILTER_SANITIZE_STRING
PHP deprecates the constant (8.1), not the filter: the ID runs without a notice.
FILTER_SANITIZE_STRIPPED
An alias of FILTER_SANITIZE_STRING (also a deprecated constant).
FILTER_SANITIZE_URL
Removes the bytes a URL cannot hold, as PHP’s FILTER_SANITIZE_URL.
FILTER_THROW_ON_FAILURE
Throws Filter\FilterFailedException on failure, as PHP’s FILTER_THROW_ON_FAILURE.
FILTER_UNSAFE_RAW
Leaves the string as is, optionally stripping or encoding bytes, as PHP’s FILTER_UNSAFE_RAW.
FILTER_VALIDATE_BOOL
Validates a boolean ("1", "true", "on", "yes" and their opposites), as PHP’s FILTER_VALIDATE_BOOL.
FILTER_VALIDATE_DOMAIN
Validates a domain name, as PHP’s FILTER_VALIDATE_DOMAIN.
FILTER_VALIDATE_EMAIL
Validates an email address, as PHP’s FILTER_VALIDATE_EMAIL.
FILTER_VALIDATE_FLOAT
Validates a float, as PHP’s FILTER_VALIDATE_FLOAT.
FILTER_VALIDATE_INT
Validates an integer, as PHP’s FILTER_VALIDATE_INT.
FILTER_VALIDATE_IP
Validates an IPv4 or IPv6 address, as PHP’s FILTER_VALIDATE_IP.
FILTER_VALIDATE_MAC
Validates a MAC address, as PHP’s FILTER_VALIDATE_MAC.
FILTER_VALIDATE_REGEXP
Validates against the regexp option, as PHP’s FILTER_VALIDATE_REGEXP.
FILTER_VALIDATE_URL
Validates a URL, as PHP’s FILTER_VALIDATE_URL.

Traits§

Callables
The PHP callables FILTER_CALLBACK can call, named by its "options" entry.

Functions§

filter_var
filter_var($value, $filter, $options), without callables (see filter_var_with).
filter_var_with
filter_var($value, $filter, $options) where callables resolves and calls the callable of FILTER_CALLBACK (the "options" entry).
validate_domain
php_filter_validate_domain_ex(): whether a non-empty string passes filter_var($d, FILTER_VALIDATE_DOMAIN), with FILTER_FLAG_HOSTNAME when hostname is set, without building the filter’s result.
validate_ip
php_filter_validate_ip(): whether filter_var($s, FILTER_VALIDATE_IP, $flags) of a string succeeds, without the copy of the string it returns.
validate_url
php_filter_validate_url(): filter_var($s, FILTER_VALIDATE_URL, $flags) of a string, without the copy of the string it returns; Err for an invalid uri_parser_class.