PHP code example of atharhashmi / sanitizer
1. Go to this page and download the library: Download atharhashmi/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/ */
atharhashmi / 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 \Waavi\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',
];
class RemoveStringsFilter implements Waavi\Sanitizer\Contracts\Filter
{
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' => [
...
Waavi\Sanitizer\Laravel\SanitizerServiceProvider::class,
];
'aliases' => [
...
'Sanitizer' => Waavi\Sanitizer\Laravel\Facade::class,
];
$newData = \Sanitizer::make($data, $filters)->sanitize();
\Sanitizer::extend($filterName, $closureOrClassPath);
namespace App\Http\Requests;
use App\Http\Requests\Request;
use Waavi\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,
];
}
/* ... */