svn: /phpdoc/en/trunk/reference/filter/ examples.xml functions/filter-var.xml

From: Date: Sun, 16 Aug 2009 23:13:38 +0000
Subject: svn: /phpdoc/en/trunk/reference/filter/ examples.xml functions/filter-var.xml
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-4698@lists.php.net to get a copy of this message
cellog Sun, 16 Aug 2009 23:13:38 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=287382 Log: fix up crap-ass documentation of 3rd parameter to filter_var with some examples Changed paths: U phpdoc/en/trunk/reference/filter/examples.xml U phpdoc/en/trunk/reference/filter/functions/filter-var.xml Modified: phpdoc/en/trunk/reference/filter/examples.xml =================================================================== --- phpdoc/en/trunk/reference/filter/examples.xml 2009-08-16 22:35:12 UTC (rev 287381) +++ phpdoc/en/trunk/reference/filter/examples.xml 2009-08-16 23:13:38 UTC (rev 287382) @@ -59,6 +59,48 @@ </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)) { + echo "This (int_a) integer is considered valid (between 0 and 3).\n"; +} +if (filter_var($int_b, FILTER_VALIDATE_INT, $options)) { + echo "This (int_b) integer is considered valid (between 0 and 3).\n"; +} +if (filter_var($int_c, FILTER_VALIDATE_INT, $options)) { + 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)) { + 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. +]]> + </screen> + </example> + </para> </section> <section xml:id='filter.examples.sanitization'> Modified: phpdoc/en/trunk/reference/filter/functions/filter-var.xml =================================================================== --- phpdoc/en/trunk/reference/filter/functions/filter-var.xml 2009-08-16 22:35:12 UTC (rev 287381) +++ phpdoc/en/trunk/reference/filter/functions/filter-var.xml 2009-08-16 23:13:38 UTC (rev 287382) @@ -43,8 +43,45 @@ <para> Associative array of options or bitwise disjunction of flags. If filter accepts options, flags can be provided in "flags" field of array. For - the "callback" filter, <type>callback</type> type should be passed. + the "callback" filter, <type>callback</type> type should be passed. The + callback must accept one argument, the value to be filtered, and return + the value after filtering/sanitizing it. </para> + <para> + <programlisting role="php"> + <![CDATA[ +<?php +// for filters that accept options, use this format +$options = array( + 'options' => array( + 'default' => 3, // value to return if the filter fails + // other options here + 'min_range' => 0 + ), + 'flags' => FILTER_FLAG_ALLOW_OCTAL, +); +$var = filter_var('0755', FILTER_VALIDATE_INT, $options); + +// for filter that only accept flags, you can pass them directly +$var = filter_var('oops', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); + +// for filter that only accept flags, you can also pass as an array +$var = filter_var('oops', FILTER_VALIDATE_BOOLEAN, + array('flags' => FILTER_NULL_ON_FAILURE)); + +// callback filter +function foo($value) +{ + $ret = new stdClass; + $ret->value = filter_var($value, FILTER_VALIDATE_BOOLEAN, + array('flags' => FILTER_NULL_ON_FAILURE)); + return $ret; +} +$var = filter_var('yes', FILTER_CALLBACK, array('options' => 'foo')); +?> + ]]> + </programlisting> + </para> </listitem> </varlistentry> </variablelist>

« previous php.doc.cvs (#4698) next »