PHP code example of gustavonecore / php-sanitizer
1. Go to this page and download the library: Download gustavonecore/php-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/ */
gustavonecore / php-sanitizer example snippets
$filter = new Gcore\Sanitizer\Template\TemplateSanitizer([
'first_name' => 'string',
'dob' => 'int',
'numbers' => 'int[]',
'test' => 'int',
'email' => 'email',
'email_wrong' => 'email',
'double' => 'double',
'boolean' => 'bool',
'datetime' => 'datetime',
"persons[]" => [
'name' => 'string',
'eyes' => 'int',
'address' => 'string[]',
'commits[]' => [
'hash' => 'string',
'n_comments' => 'int',
'users' => 'string[]'
],
],
]);
$filter = new Gcore\Sanitizer\Template\TemplateSanitizer([
'email' => 'email!',
'phone' => 'string!',
'first_name' => 'string',
]);
// This will be your inut body from an user
$input = [
'first_name' => 'Gustavo',
'dob' => '10',
'numbers' => [1,2,3,'4','5'],
'foo' => [1,2,4],
'email' => '[email protected] ',
'email_wrong' => 'gustavo.uachgmail.com',
'double' => '7876',
'boolean' => true,
'datetime' => '2017-11-11 13:50:10',
'persons' => [
[
'name' => 'jhon',
'eyes' => 2,
'address' => ['foo', 'bar', 'text'],
'commits' => [
'hash' => '2221321n3kj12n3kj12n32j1',
'n_comments' => 900,
'users' => ['jhon', 'doe'],
],
],
[
'name' => 'albert',
'eyes' => 'wrong int here',
'address' => ['a', 'b', 1],
'commits' => null,
],
]
];
// All your data is clean now! awesome!
$cleanOutput = $filter->sanitize($input);
print_r($cleanOutput);
php examples/index.php
Array
(
[first_name] => Gustavo
[dob] => 10
[numbers] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
[test] =>
[email] => [email protected]
[email_wrong] =>
[double] => 7876
[boolean] => 1
[datetime] => DateTimeImmutable Object
(
[date] => 2017-11-11 13:50:10.000000
[timezone_type] => 3
[timezone] => America/Santiago
)
[persons] => Array
(
[0] => Array
(
[name] => jhon
[eyes] => 2
[address] => Array
(
[0] => foo
[1] => bar
[2] => text
)
[commits] => Array
(
[hash] => 2221321n3kj12n3kj12n32j1
[n_comments] => 900
[users] => Array
(
[0] => jhon
[1] => doe
)
)
)
[1] => Array
(
[name] => albert
[eyes] =>
[address] => Array
(
[0] => a
[1] => b
[2] => 1
)
[commits] =>
)
)
)