Forked from franz-josef-kaiser/filter_var_array.php
Created
February 26, 2016 21:34
-
-
Save puiutucutu/3724b0199dccf9d75ceb to your computer and use it in GitHub Desktop.
PHP filter_var_array() example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace WCM; | |
# USES PHP 5.3 + Closures | |
// No white surrounding space | |
$data = array_map( 'trim', $_POST['foo'] ); | |
$data = filter_var_array( $data, array( | |
'alpha_plus_period_plus_dash' => array( | |
'filter' => FILTER_CALLBACK, | |
'options' => function( $title ) { | |
return preg_replace( '/[^a-zA-Z.-]/', "", $title ); | |
}, | |
), | |
'boolean' => FILTER_VALIDATE_BOOLEAN, | |
'name_first' => array( | |
'filter' => FILTER_CALLBACK, | |
'options' => array( | |
$this, 'sanitizeString' | |
), | |
), | |
'name_last' => array( | |
'filter' => FILTER_CALLBACK, | |
'options' => array( | |
$this, 'sanitizeString' | |
), | |
), | |
// Phone Number of format: 1st char = digit (digit: 0-9)(dash)(digit: 0-9) | |
'phone' => array( | |
'filter' => FILTER_CALLBACK, | |
'options' => function( $nr ) { | |
return preg_replace( '/[^\d0-9]-[\d0-9]/i', "", $nr ); | |
}, | |
), | |
'email' => FILTER_SANITIZE_EMAIL, | |
) ); | |
// Don't save empty values | |
$data = array_filter( $data ); | |
/** | |
* Callback to sanitize a string of the format (a-z)(possible dash)(a-z) | |
* @param string $string | |
* @return string Sanitized string | |
*/ | |
function sanitizeString( $string ) | |
{ | |
return preg_replace( '/[^a-z]-[a-z]/i', "", $string ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment