Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP filter_var_array() example
<?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
You can’t perform that action at this time.