mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-15 16:38:54 +00:00

Based on a patch by an anonymous online editor user git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@343433 c90b9560-bf6c-de11-be94-00142212c4b1
189 lines
4.8 KiB
XML
189 lines
4.8 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
|
|
<chapter xml:id="filter.examples" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
&reftitle.examples;
|
|
|
|
<section xml:id="filter.examples.validation">
|
|
<title>Validation</title>
|
|
<para>
|
|
<example>
|
|
<title>Validating email addresses with <function>filter_var</function></title>
|
|
<programlisting role="php" xml:id="filter.examples.validation.email">
|
|
<![CDATA[
|
|
<?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";
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
Email address 'joe@example.com' is considered valid.
|
|
Email address 'bogus' is considered invalid.
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
|
|
<para>
|
|
<example>
|
|
<title>Validating IP addresses with <function>filter_var</function></title>
|
|
<programlisting role="php" xml:id="filter.examples.validation.ip">
|
|
<![CDATA[
|
|
<?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.";
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
IP address '127.0.0.1' is considered valid.
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
|
|
<para>
|
|
<example>
|
|
<title>Passing options to <function>filter_var</function></title>
|
|
<programlisting role="php" xml:id="filter.examples.validation.options">
|
|
<![CDATA[
|
|
<?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).";
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
Integer A '1' is considered valid (between 0 and 3).
|
|
Integer C '1' is considered valid (between 0 and 3).
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
</section>
|
|
|
|
<section xml:id="filter.examples.sanitization">
|
|
<title>Sanitization</title>
|
|
<para>
|
|
<example>
|
|
<title>Sanitizing and validating email addresses</title>
|
|
<programlisting role="php" xml:id="filter.examples.sanitization.email">
|
|
<![CDATA[
|
|
<?php
|
|
$a = 'joe@example.org';
|
|
$b = 'bogus - at - example dot org';
|
|
$c = '(bogus@example.org)';
|
|
|
|
$sanitized_a = filter_var($a, FILTER_SANITIZE_EMAIL);
|
|
if (filter_var($sanitized_a, FILTER_VALIDATE_EMAIL)) {
|
|
echo "This (a) sanitized email address is considered valid.\n";
|
|
}
|
|
|
|
$sanitized_b = filter_var($b, FILTER_SANITIZE_EMAIL);
|
|
if (filter_var($sanitized_b, FILTER_VALIDATE_EMAIL)) {
|
|
echo "This sanitized email address is considered valid.";
|
|
} else {
|
|
echo "This (b) sanitized email address is considered invalid.\n";
|
|
}
|
|
|
|
$sanitized_c = filter_var($c, FILTER_SANITIZE_EMAIL);
|
|
if (filter_var($sanitized_c, FILTER_VALIDATE_EMAIL)) {
|
|
echo "This (c) sanitized email address is considered valid.\n";
|
|
echo "Before: $c\n";
|
|
echo "After: $sanitized_c\n";
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
This (a) sanitized email address is considered valid.
|
|
This (b) sanitized email address is considered invalid.
|
|
This (c) sanitized email address is considered valid.
|
|
Before: (bogus@example.org)
|
|
After: bogus@example.org
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Configuring a default filter</title>
|
|
<programlisting role="php" xml:id="filter.examples.sanitization.default_filter">
|
|
<![CDATA[
|
|
filter.default = full_special_chars
|
|
filter.default_flags = 0
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
</section>
|
|
|
|
</chapter>
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|
|
|