PHP filter_var()Functions


PHP Filter 参考手册Complete PHP Filter Reference Manual

Definition and Usage

The filter_var() function filters a variable with the specified filter.

Returns the filtered data on success, or FALSE on failure.

Syntax

filter_var(variable, filter, options)

Parameter Description
variable Required. Specifies the variable to filter.
filter Optional. Specifies the filter ID to use. Default is FILTER_SANITIZE_STRING. SeeComplete PHP Filter Reference Manualfor possible filters.

The filter ID can be an ID name (such as FILTER_VALIDATE_EMAIL) or an ID number (such as 274).

options Optional. Specifies an associative array containing flags/options or a single flag/option. Check the possible flags and options for each filter.


Example

<?php
if(!filter_var("someone@example....com", FILTER_VALIDATE_EMAIL))
{
echo("E-mail is not valid");
}
else
{
echo("E-mail is valid");
}
?>

The output of the code is as follows:

E-mail is not valid


PHP Filter 参考手册Complete PHP Filter Reference Manual Other Extensions