array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'uk', ), 'this' => array ( 0 => 'filter.examples.validation.php', 1 => 'Validation', 2 => 'Validation', ), 'up' => array ( 0 => 'filter.examples.php', 1 => 'Приклади', ), 'prev' => array ( 0 => 'filter.examples.php', 1 => 'Приклади', ), 'next' => array ( 0 => 'filter.examples.sanitization.php', 1 => 'Sanitization', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/filter/examples.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?>

Validation

Приклад #1 Validating email addresses with filter_var()

<?php
$email_a = 'joe@example.com';
$email_b = 'bogus';

if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
echo "Email address '$email_a' is considered valid.\n";
}
if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
echo "Email address '$email_b' is considered valid.\n";
} else {
echo "Email address '$email_b' is considered invalid.\n";
}
?>

Поданий вище приклад виведе:

Email address 'joe@example.com' is considered valid.
Email address 'bogus' is considered invalid.

Приклад #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 "IP address '$ip_a' is considered valid.";
}
if (filter_var($ip_b, FILTER_VALIDATE_IP)) {
echo "IP address '$ip_b' is considered valid.";
}
?>

Поданий вище приклад виведе:

IP address '127.0.0.1' is considered valid.

Приклад #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 "Integer A '$int_a' is considered valid (between 0 and 3).\n";
}
if (filter_var($int_b, FILTER_VALIDATE_INT, $options) !== FALSE) {
echo "Integer B '$int_b' is considered valid (between 0 and 3).\n";
}
if (filter_var($int_c, FILTER_VALIDATE_INT, $options) !== FALSE) {
echo "Integer C '$int_c' 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 "Integer C '$int_c' is considered valid (between 0 and 3).";
}
?>

Поданий вище приклад виведе:

Integer A '1' is considered valid (between 0 and 3).
Integer C '1' is considered valid (between 0 and 3).