PHP code example of fernandozueet / php-sanitize

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

    

fernandozueet / php-sanitize example snippets


use FzPhpSanitize\Sanitize;

//values array
$data = [
    'title'   => 'Test Test é 123',
    'content' => "<a href=''>teste</a> <b>OK</b>",
    'test'    => "value test",
    'date'    => "01/06/1987",
    'sub'     => [
        "sub1" => "  TEST  "
    ],
];

//rules sanitize
$rules = [
    'title'    => [Sanitize::strtolower(), Sanitize::alpha(true), Sanitize::strtoupper(), Sanitize::rtrim()],
    'content'  => [Sanitize::stripTags('<a>') ],
    'date'     => [Sanitize::date('Y-m-d')],
    'sub.sub1' => [Sanitize::strtolower(), Sanitize::trim()],
];

//sanitize values
$values = Sanitize::clear($data, $rules);

use FzPhpSanitize\Sanitize;

//sanitize
$value = Sanitize::cpf()->clean('43740999055');



namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use FzPhpSanitize\Sanitize;

class ExampleRequest extends FormRequest
{

    /**
     * Prepare the data for validation.
     *
     * @return void
     */
    protected function prepareForValidation()
    {
        $rules = [
            'title'   => [Sanitize::strtolower(), Sanitize::alpha(true), Sanitize::strtoupper(), Sanitize::rtrim()],
            'content' => [Sanitize::stripTags('<a>') ],
        ];

        $this->merge(Sanitize::clear($this->input(), $rules));
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [];
    } 

}

MyFilter.php



namespace Filters\MyFilter; // <<<<<<<<<-- Your namespace here

use FzPhpSanitize\Contracts\Filter;
use FzPhpSanitize\Filters\Filters;

class MyFilter extends Filters implements Filter
{
  
    /**
     * Filter strip tags.
     * Strip HTML and PHP tags from a string.
     * 
     * @param string $value
     * @return string
     */
    public function clean($value)
    {  
       return is_string($value) ? strip_tags($value, $this->options[0] ?? null) : "";
    }
    
}

MySanitizes.php



namespace YourNamespace; // <<<<<<<<<-- Your namespace here

use Filters\MyFilter;

class MySanitizes 
{
  
    /**
     * Filter strip_tags.
     * Strip HTML and PHP tags from a string.
     *
     * @param array|string $allowable_tags
     * @return MyFilter
     */
    public static function myFilter($allowable_tags = ""): MyFilter
    {
        return new MyFilter($allowable_tags);
    }
    
}

use YourNamespace\MySanitizes;

//sanitize
$value = MySanitizes::myFilter("<a>")->clean("<a href='#'>Link</a> <h1>Hello world!</h1>");

use FzPhpSanitize\Sanitize;

$value = Sanitize::striptags("<a>")->clean("<a href='#'>Link</a> <h1>Hello world!</h1>");

<a href='#'>Link</a> Hello world!

use FzPhpSanitize\Sanitize;

$value = Sanitize::cnpj()->clean("54465939000150");

54.465.939/0001-50

use FzPhpSanitize\Sanitize;

$value = Sanitize::cpf()->clean("43740999055");

437.409.990-55

use FzPhpSanitize\Sanitize;

$value = Sanitize::numeric()->clean("asdfg123456");

123456

use FzPhpSanitize\Sanitize;

$value = Sanitize::alphanumeric()->clean("!@#asdfg123456");

$value2 = Sanitize::alphanumeric(true)->clean("!@#asdfg 123 456");

//value
asdfg123456

//value2
asdfg 123 456

use FzPhpSanitize\Sanitize;

$value = Sanitize::alpha()->clean("123456asdfg*&(");

$value2 = Sanitize::alpha(true)->clean("123456asd dfg*&(");

//value
asdfg

//value2
asd dfg

use FzPhpSanitize\Sanitize;

$value = Sanitize::url()->clean("http://php.net/manual/en/function.htmlentities.phpçù");

http://php.net/manual/en/function.htmlentities.php

use FzPhpSanitize\Sanitize;

$value = Sanitize::email()->clean("çó[email protected]");

[email protected]

use FzPhpSanitize\Sanitize;

$value = Sanitize::strtolower()->clean("FERNANDO ZUEET");

fernando zueet

use FzPhpSanitize\Sanitize;

$value = Sanitize::strtoupper()->clean("fernando zueet");

FERNANDO ZUEET

use FzPhpSanitize\Sanitize;

$value = Sanitize::ucwords()->clean("fernando zueet");

Fernando Zueet

use FzPhpSanitize\Sanitize;

$value = Sanitize::ucfirst()->clean("fernando zueet");

Fernando zueet

use FzPhpSanitize\Sanitize;

$value = Sanitize::lcfirst()->clean("Fernando zueet");

fernando zueet

use FzPhpSanitize\Sanitize;

$value = Sanitize::rtrim()->clean("fernando zueet    ");

fernando zueet

use FzPhpSanitize\Sanitize;

$value = Sanitize::ltrim()->clean("     fernando zueet");

fernando zueet

use FzPhpSanitize\Sanitize;

$value = Sanitize::trim()->clean("     fernando zueet    ");

fernando zueet

use FzPhpSanitize\Sanitize;

$value = Sanitize::date("Y-m-d")->clean("01/06/1987");

1987-06-01

use FzPhpSanitize\Sanitize;

$value = Sanitize::type('string')->clean(10);

$value2 = Sanitize::type('bool')->clean('true');

$value3 = Sanitize::type('int')->clean('1234');

$value4 = Sanitize::type('float')->clean('100,5');

//value
'10' 

//value2
true

//value3
1234

//value4
100.5

use FzPhpSanitize\Sanitize;

$value = Sanitize::numberFormat(2, ',', '.')->clean("1000");

1.000,00

use FzPhpSanitize\Sanitize;

$value = Sanitize::pregReplace('/[^A-Za-z]/', '')->clean("1234asdfg");

asdfg

use FzPhpSanitize\Sanitize;

$value = Sanitize::filterVar(FILTER_SANITIZE_EMAIL)->clean("çó[email protected]");

[email protected]
bash
composer