PHP code example of binary-cats / sanitizer
1. Go to this page and download the library: Download binary-cats/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/ */
binary-cats / sanitizer example snippets
$data = [
'first_name' => 'john',
'last_name' => '<strong>DOE</strong>',
'email' => ' [email protected] ',
'birthdate' => '06/25/1980',
'jsonVar' => '{"name":"value"}',
'description' => '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>',
'phone' => '+08(096)90-123-45q',
'country' => 'GB',
'postcode' => 'ab12 3de',
];
use BinaryCats\Sanitizer\Sanitizer;
$filters = [
'first_name' => 'trim|escape|capitalize',
'last_name' => 'trim|escape|capitalize',
'email' => 'trim|escape|lowercase',
'birthdate' => 'trim|format_date:m/d/Y, Y-m-d',
'jsonVar' => 'cast:array',
'description' => 'strip_tags',
'phone' => 'digit',
'country' => 'trim|escape|capitalize',
'postcode' => 'trim|escape|uppercase|filter_if:country,GB',
];
$sanitizer = new Sanitizer($data, $filters);
var_dump($sanitizer->sanitize());
[
'first_name' => 'John',
'last_name' => 'Doe',
'email' => '[email protected] ',
'birthdate' => '1980-06-25',
'jsonVar' => '["name" => "value"]',
'description' => 'Test paragraph. Other text',
'phone' => '080969012345',
'country' => 'GB',
'postcode' => 'AB12 3DE',
];
use BinaryCats\Sanitizer\Contracts\Filter;
class RemoveStringsFilter implements Filter
{
/**
* Apply filter
*
* @param mixed $value
* @param array $options
* @return string
*/
public function apply($value, $options = [])
{
return str_replace($options, '', $value);
}
}
$customFilters = [
'hash' => function($value, $options = []) {
return sha1($value);
},
'remove_strings' => RemoveStringsFilter::class,
];
$filters = [
'secret' => 'hash',
'text' => 'remove_strings:Curse,Words,Galore',
];
$sanitize = new Sanitize($data, $filters, $customFilters);
'providers' => [
...
BinaryCats\Sanitizer\Laravel\SanitizerServiceProvider::class,
];
'aliases' => [
...
'Sanitizer' => BinaryCats\Sanitizer\Laravel\Facade::class,
];
$newData = \Sanitizer::make($data, $filters)->sanitize();
\Sanitizer::extend($filterName, $closureOrClassPath);
namespace App\Http\Requests;
use App\Http\Requests\Request;
use BinaryCats\Sanitizer\Laravel\SanitizesInput;
class SanitizedRequest extends Request
{
use SanitizesInput;
public function filters()
{
return [
'name' => 'trim|capitalize',
'email' => 'trim',
'text' => 'remove_strings:Curse,Words,Galore',
];
}
public function customFilters()
{
return [
'remove_strings' => RemoveStringsFilter::class,
];
}
/* ... */