PHP filter_var () Functie
Voorbeeld
Controleer of $email een geldig e-mailadres is:
<?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");
}
?>
Definitie en gebruik
De functie filter_var() filtert een variabele met het opgegeven filter.
Syntaxis
filter_var(var, filtername, options)
Parameterwaarden
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 |
Technische details
Winstwaarde: | Retourneert de gefilterde gegevens bij succes, ONWAAR bij mislukking |
---|---|
PHP-versie: | 5.2+ |
Meer voorbeelden
Het onderstaande voorbeeld reinigt en valideert een e-mailadres:
Voorbeeld
Verwijder eerst illegale tekens uit $email en controleer vervolgens of het een geldig e-mailadres is:
<?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");
}
?>
❮ PHP-filterreferentie