Przykład #1 Validating email addresses with filter_var()
<?php
$email_a = '[email protected]';
$email_b = 'bogus';
if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
echo "This (email_a) email address is considered valid.";
}
if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
echo "This (email_b) email address is considered valid.";
}
?>
Powyższy przykład wyświetli:
This (email_a) email address is considered valid.
Przykład #2 Validating IP addresses with filter_var()
<?php
$ip_a = '127.0.0.1';
$ip_b = '42.42';
if (filter_var($ip_a, FILTER_VALIDATE_IP)) {
echo "This (ip_a) IP address is considered valid.";
}
if (filter_var($ip_b, FILTER_VALIDATE_IP)) {
echo "This (ip_b) IP address is considered valid.";
}
?>
Powyższy przykład wyświetli:
This (ip_a) IP address is considered valid.
Przykład #3 Passing options to filter_var()
<?php
$int_a = '1';
$int_b = '-1';
$int_c = '4';
$options = array(
'options' => array(
'min_range' => 0,
'max_range' => 3,
)
);
if (filter_var($int_a, FILTER_VALIDATE_INT, $options) !== FALSE) {
echo "This (int_a) integer is considered valid (between 0 and 3).\n";
}
if (filter_var($int_b, FILTER_VALIDATE_INT, $options) !== FALSE) {
echo "This (int_b) integer is considered valid (between 0 and 3).\n";
}
if (filter_var($int_c, FILTER_VALIDATE_INT, $options) !== FALSE) {
echo "This (int_c) integer is considered valid (between 0 and 3).\n";
}
$options['options']['default'] = 1;
if (($int_c = filter_var($int_c, FILTER_VALIDATE_INT, $options)) !== FALSE) {
echo "This (int_c) integer is considered valid (between 0 and 3) and is $int_c.";
}
?>
Powyższy przykład wyświetli:
This (int_a) integer is considered valid (between 0 and 3). This (int_c) integer is considered valid (between 0 and 3) and is 1.