PHP code example of creativitykills / sanity

1. Go to this page and download the library: Download creativitykills/sanity 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/ */

    

creativitykills / sanity example snippets




// Possibly input from $_POST or $_GET global array
$someArray = ['name' => ' JOHN DOE ', 'email' => ' [email protected] '];

// Rules to validate against
$sanitizerRules = ['name' => 'ucwords|trim', 'email' => 'strtolower|trim'];

$sanitizer = new \CreativityKills\Sanity\Sanitizer;

$someArray = $sanitizer->sanitize($someArray, $sanitizerRules);

// array(
//    'name'  => 'John Doe',
//    'email' => '[email protected]'
// )
var_dump($someArray);



use CreativityKills\Sanity\Sanitizer;

class UserSanitizer extends Sanitizer {
    
    protected $rules = [
        'name'  => 'ucwords|trim|remove_excess_white_spaces',
        'email' => 'strtolower|trim'
    ];
    
    public function sanitizeRemoveExcessWhiteSpaces($value)
    {
        return preg_replace('/\s+/', ' ', $value)
    }
}



// Possibly input from $_POST or $_GET global array
$someArray = ['name' => ' JOHN   DOE ', 'email' => ' [email protected] '];
                
$someArray = (new UserSanitizer)->sanitize($someArray);

// array(
//    'name'  => 'John Doe',
//    'email' => '[email protected]'
// )
var_dump($someArray);