PHP filter_var() Funkcja
Przykład
Sprawdź, czy $email jest poprawnym adresem e-mail:
<?php
$email = "[email protected]";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo("$email is a valid email address");
} else {
echo("$email is not a valid email address");
}
?>
Definicja i użycie
Funkcja filter_var() filtruje zmienną z określonym filtrem.
Składnia
filter_var(var, filtername, options)
Wartości parametrów
Parameter | Description |
---|---|
var | Required. The variable to filter |
filtername | Optional. Specifies the ID or name of the filter to use. Default is FILTER_DEFAULT, which results in no filtering |
options | Optional. Specifies one or more flags/options to use. Check each filter for possible options and flags |
Szczegóły techniczne
Wartość zwrotu: | Zwraca przefiltrowane dane w przypadku sukcesu, FALSE w przypadku niepowodzenia |
---|---|
Wersja PHP: | 5.2+ |
Więcej przykładów
Poniższy przykład zarówno oczyszcza, jak i weryfikuje adres e-mail:
Przykład
Najpierw usuń niedozwolone znaki z $email, a następnie sprawdź, czy jest to poprawny adres e-mail:
<?php
$email = "[email protected]";
// Remove all illegal characters from email
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate e-mail
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo("$email is a valid email address");
} else {
echo("$email is not a valid email address");
}
?>
❮ Odniesienie do filtrów PHP