2009-04-28 20:31:51 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 07:05:15 +00:00
|
|
|
<!-- $Revision$ -->
|
2009-04-28 20:31:51 +00:00
|
|
|
|
|
|
|
<chapter xml:id="filter.examples" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
|
|
&reftitle.examples;
|
|
|
|
|
2011-09-30 14:21:51 +00:00
|
|
|
<section xml:id="filter.examples.validation">
|
2009-04-28 20:31:51 +00:00
|
|
|
<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)) {
|
2013-11-16 23:21:21 +00:00
|
|
|
echo "This ($email_a) email address is considered valid.";
|
2009-04-28 20:31:51 +00:00
|
|
|
}
|
|
|
|
if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
|
2013-11-16 23:21:21 +00:00
|
|
|
echo "This ($email_b) email address is considered valid.";
|
2009-04-28 20:31:51 +00:00
|
|
|
}
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
&example.outputs;
|
|
|
|
<screen>
|
|
|
|
<![CDATA[
|
2013-11-16 23:21:21 +00:00
|
|
|
This (joe@example.com) email address is considered valid.
|
2009-04-28 20:31:51 +00:00
|
|
|
]]>
|
|
|
|
</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 "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.";
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
&example.outputs;
|
|
|
|
<screen>
|
|
|
|
<![CDATA[
|
|
|
|
This (ip_a) IP address is considered valid.
|
2009-08-16 23:13:38 +00:00
|
|
|
]]>
|
|
|
|
</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,
|
|
|
|
)
|
|
|
|
);
|
2009-09-25 11:10:55 +00:00
|
|
|
if (filter_var($int_a, FILTER_VALIDATE_INT, $options) !== FALSE) {
|
2009-08-16 23:13:38 +00:00
|
|
|
echo "This (int_a) integer is considered valid (between 0 and 3).\n";
|
|
|
|
}
|
2009-09-25 11:10:55 +00:00
|
|
|
if (filter_var($int_b, FILTER_VALIDATE_INT, $options) !== FALSE) {
|
2009-08-16 23:13:38 +00:00
|
|
|
echo "This (int_b) integer is considered valid (between 0 and 3).\n";
|
|
|
|
}
|
2009-09-25 11:10:55 +00:00
|
|
|
if (filter_var($int_c, FILTER_VALIDATE_INT, $options) !== FALSE) {
|
2009-08-16 23:13:38 +00:00
|
|
|
echo "This (int_c) integer is considered valid (between 0 and 3).\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$options['options']['default'] = 1;
|
2009-09-25 11:10:55 +00:00
|
|
|
if (($int_c = filter_var($int_c, FILTER_VALIDATE_INT, $options)) !== FALSE) {
|
2009-08-16 23:13:38 +00:00
|
|
|
echo "This (int_c) integer is considered valid (between 0 and 3) and is $int_c.";
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
&example.outputs;
|
|
|
|
<screen>
|
|
|
|
<![CDATA[
|
|
|
|
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.
|
2009-04-28 20:31:51 +00:00
|
|
|
]]>
|
|
|
|
</screen>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
</section>
|
|
|
|
|
2011-09-30 14:21:51 +00:00
|
|
|
<section xml:id="filter.examples.sanitization">
|
2009-04-28 20:31:51 +00:00
|
|
|
<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>
|
2012-02-20 09:27:48 +00:00
|
|
|
<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>
|
2009-04-28 20:31:51 +00:00
|
|
|
</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
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2009-04-28 20:31:51 +00:00
|
|
|
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
|
|
|
|
-->
|
|
|
|
|