fernandozueet/php-sanitize

Sanitize php values

1.0.1 2020-10-29 18:43 UTC

This package is auto-updated.

Last update: 2024-04-29 04:40:23 UTC


README

Sanitize php values.



Documentation



Requirements

  • PHP 7.3 or superior
  • Composer


Installation

Install this package with composer:

composer require fernandozueet/php-sanitize


Mode of use Array

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);

Output:

{
    "title": "TEST TEST",
    "content": "<a href=''>teste</a> OK",
    "teste": "value test",
    "date": "1987-06-01",
    "sub": {
        "sub1": "test"
    }
}


Mode of use Individual

use FzPhpSanitize\Sanitize;

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

Output:

437.409.990-55


Mode of use Laravel

Laravel 5.8 or superior

<?php

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 [];
    } 

}


Custom filter

1- Create class filter

MyFilter.php

<?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) : "";
    }
    
}

2- Create a function in another pho file to call the filter

MySanitizes.php

<?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);
    }
    
}

3- Use filter

use YourNamespace\MySanitizes;

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

Output:

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


Filters



striptags

Strip HTML and PHP tags from a string.

striptags(string $allowable_tags = "")

use FzPhpSanitize\Sanitize;

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

Output:

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


cnpj

Format the cnpj format number.

cnpj()

use FzPhpSanitize\Sanitize;

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

Output:

54.465.939/0001-50


cpf

Format the cpf format number.

cpf()

use FzPhpSanitize\Sanitize;

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

Output:

437.409.990-55


numeric

Numbers.

numeric()

use FzPhpSanitize\Sanitize;

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

Output:

123456


alphanumeric

Letters from a to z and numbers.

alphanumeric(bool $spaces = false)

use FzPhpSanitize\Sanitize;

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

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

Output:

//value
asdfg123456

//value2
asdfg 123 456


alpha

Letters from a to z.

alpha(bool $spaces = false)

use FzPhpSanitize\Sanitize;

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

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

Output:

//value
asdfg

//value2
asd dfg


url

filter_var FILTER_SANITIZE_URL

url()

use FzPhpSanitize\Sanitize;

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

Output:

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


email

filter_var FILTER_SANITIZE_EMAIL

email()

use FzPhpSanitize\Sanitize;

$value = Sanitize::email()->clean("çótest@test.com");

Output:

test@test.com


strtolower

Make a string lowercase.

strtolower()

use FzPhpSanitize\Sanitize;

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

Output:

fernando zueet


strtoupper

Make a string uppercase.

strtoupper()

use FzPhpSanitize\Sanitize;

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

Output:

FERNANDO ZUEET


ucwords

Uppercase the first character of each word in a string.

ucwords(string $delimiters = " \t\r\n\f\v")

use FzPhpSanitize\Sanitize;

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

Output:

Fernando Zueet


ucfirst

Make a string's first character uppercase.

ucfirst()

use FzPhpSanitize\Sanitize;

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

Output:

Fernando zueet


lcfirst

Make a string's first character lowercase.

lcfirst()

use FzPhpSanitize\Sanitize;

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

Output:

fernando zueet


rtrim

Removes blanks (or other characters) from the beginning of the string.

rtrim(string $charlist = " \t\n\r\0\x0B")

use FzPhpSanitize\Sanitize;

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

Output:

fernando zueet


ltrim

Removes blanks (or other characters) from the beginning of the string.

ltrim(string $charlist = " \t\n\r\0\x0B")

use FzPhpSanitize\Sanitize;

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

Output:

fernando zueet


trim

Removing space at the beginning and end of a string.

trim(string $charlist = " \t\n\r\0\x0B")

use FzPhpSanitize\Sanitize;

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

Output:

fernando zueet


date

Date format.

date(string $format = 'Y-m-d')

use FzPhpSanitize\Sanitize;

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

Output:

1987-06-01


type

Format a types.

type(string $type)

$type: string bool int float array object

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');

Output:

//value
'10' 

//value2
true

//value3
1234

//value4
100.5


numberFormat

Format a number with grouped thousands.

numberFormat(int $decimals = 0, string $decimalpoint = '.', string $separator = ',')

use FzPhpSanitize\Sanitize;

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

Output:

1.000,00


pregReplace

Perform a regular expression search and replace.

pregReplace($pattern, $replacement)

http://php.net/manual/en/function.preg-replace.php

use FzPhpSanitize\Sanitize;

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

Output:

asdfg


filterVar

Filters a variable with a specified filter.

filterVar(int $filter = FILTER_DEFAULT, $options = null)

http://php.net/manual/en/function.filter-var.php

use FzPhpSanitize\Sanitize;

$value = Sanitize::filterVar(FILTER_SANITIZE_EMAIL)->clean("çótest@test.com");
test@test.com


Contributing

Please see CONTRIBUTING for details.

Security

If you discover security related issues, please email fernandozueet@hotmail.com instead of using the issue tracker.

Credits

License

The FZ Php Sanitize is licensed under the MIT license. See License File for more information.