PHP code example of raditzfarhan / yii2-sanitizer

1. Go to this page and download the library: Download raditzfarhan/yii2-sanitizer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

raditzfarhan / yii2-sanitizer example snippets


'components' => [
    ...
    'sanitizer' => [           
        'class' => 'raditzfarhan\Yii2Sanitizer\Sanitize',                   
    ],
    ...
],

// data to be filtered
$data = [
    'name' => ' Farhan"',
    'address' => '<p>No 1, Residence ABC</p>',
    'postcode' => '81221A',
    'points' => '-152.1711B',
    'status' => 'C1',
];

// create a filters corresponding to the input data array
$filters = [
    'name' => ['trim', 'escape'],
    'address' => ['trim', 'escape', 'cast:string'],
    'postcode' => ['digit'],
    'points' => ['digit'],
    'status' => ['cast:int'],
];

// call sanitize function to filter an array of inputs
$filtered_data = Yii::$app->sanitizer->sanitize($data, $filters);
var_dump($filtered_data);

[
    'name' => 'Farhan\\\"'
    'address' => 'No 1, Residence ABC'
    'postcode' => 81221
    'points' => -152.1711
    'status' => '1'
]

// call filter function to filter a single value. You can add filter type as the second argument.
$filtered_data = Yii::$app->sanitizer->filter('<p>No 1, Residence ABC</p>', ['trim', 'cast:string']);
echo $filtered_data;

No 1, Residence ABC