filter php code example

Example 1: array filter use key

$my_array = ['foo' => 1, 'hello' => 'world'];
$allowed  = ['foo', 'bar'];
$filtered = array_filter(
    $my_array,
    function ($key) use ($allowed) {
        return in_array($key, $allowed);
    },
    ARRAY_FILTER_USE_KEY
);

Example 2: sanitize form data php

# sanitize form data
function clean($data)
{
    $data = htmlspecialchars($data);
    $data = stripslashes($data);
    $data = trim($data);
    return $data;
}

Example 3: php array filter syntax

$numbers = [2, 4, 6, 8, 10];

function MyFunction($number)
{
  return $number > 5;
}

$filteredArray = array_filter($numbers, "MyFunction");

/**
 * `$filteredArray` now contains: `[6, 8, 10]`
 * NB: Use this to remove what you don't want in the array
 * @see `array_map` when you want to alter/change elements
 * in the array.
 */

Example 4: php array filter

<?php

$arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4];

var_dump(array_filter($arr, function($k) {
    return $k == 'b';
}, ARRAY_FILTER_USE_KEY));

var_dump(array_filter($arr, function($v, $k) {
    return $k == 'b' || $v == 4;
}, ARRAY_FILTER_USE_BOTH));
?>

Example 5: php filter array

$numbers = [-2, 4, -6, 8, 10];

function isPositive($number)
{
  return $number > 0;
}

$filteredArray = array_filter($numbers, "isPositive");

Example 6: php filters

#Filter Validation

<?php
    //checks for posted data
    /*
    if(filter_has_var(INPUT_POST,'data')){
        echo 'Data Found';
    }else{
        echo 'No Data';
    }
    */
    if(filter_has_var(INPUT_POST,'data')){
        //remove illegal chars
        $email = $_POST['data'];
        $email = filter_var($email, FILTER_SANITIZE_EMAIL);
        echo $email.'<br>';

     //validate as an email address
     if(filter_input(INPUT_POST, 'data', FILTER_VALIDATE_EMAIL)){
        echo 'Email is Valid';
     }else{
        echo 'Email is not Valid';
     }
    
     //we can use the below if we use the sanatizer above
     if(filter_var($email, FILTER_VALIDATE_EMAIL)){
        echo 'Email is Valid';
     }else{
        echo 'Email is not Valid';
    }
}
    #Other Validations
    #FILTER_VALIDATE_BOOLEAN
    #FILTER_VALIDATE_EMAIL
    #FILTER_VALIDATE_FLOAT
    #FILTER_VALIDATE_INT
    #FILTER_VALIDATE_IP
    #FILTER_VALIDATE_REGEXP  'REGULAR EXPRESSION'
    #FILTER_VALIDATE_URL

    #Other sanatizes
    #FILTER_SANATIZE_EMAIL
    #FILTER_SANATIZE_ENCODED
    #FILTER_SANATIZE_NUMBER_FLOAT
    #FILTER_SANATIZE_NUMBER_INT
    #FILTER_SANATIZE_SPECIAL_CHARS
    #FILTER_SANATIZE_STRING
    #FILTER_SANATIZE_URL

    //EXAMPLE int VALIDATION
    $var = 'john';
    if(filter_var($var, FILTER_VALIDATE_INT)){
        echo '<br>'.$var.' is a number<br>';
    }else{
        echo '<br>'.$var.' is not a number'.'<br>';
    }

    //EXAMPLE int Sanitazion
    $var2 = '33k2dsdffgsdf3563sdf';
    var_dump(filter_var($var2, FILTER_SANITIZE_NUMBER_INT));

    //ARRAY USAGE
    $filters = array(
        "data" => FILTER_VALIDATE_EMAIL,
        "data2" => array(
            "filter" => FILTER_VALIDATE_INT,
            "options" => array(
                "min_range" => 1,
                "max_range" => 100
        )
        )
    );
    print_r(filter_input_array(INPUT_POST, $filters));

?>

<form method ="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    <input type="text" name="data">
    <input type="text" name="data2">
    <button type="submit">Submit</button>
</form>

Tags:

Php Example