Sanitize php values.
- Requirements
- Installation
- Mode of use Array
- Mode of use Individual
- Mode of use Laravel
- Custom filter
- Filter striptags
- Filter cnpj
- Filter cpf
- Filter numeric
- Filter alphanumeric
- Filter alpha
- Filter url
- Filter email
- Filter strtolower
- Filter strtoupper
- Filter ucwords
- Filter ucfirst
- Filter lcfirst
- Filter rtrim
- Filter ltrim
- Filter trim
- Filter date
- Filter type
- Filter numberFormat
- Filter pregReplace
- Filter filterVar
- Contributing
- Security
- Credits
- License
- PHP 7.3 or superior
- Composer
Install this package with composer:
composer require fernandozueet/php-sanitize
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"
}
}
use FzPhpSanitize\Sanitize;
//sanitize
$value = Sanitize::cpf()->clean('43740999055');
Output:
437.409.990-55
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 [];
}
}
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!
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!
Format the cnpj format number.
cnpj()
use FzPhpSanitize\Sanitize;
$value = Sanitize::cnpj()->clean("54465939000150");
Output:
54.465.939/0001-50
Format the cpf format number.
cpf()
use FzPhpSanitize\Sanitize;
$value = Sanitize::cpf()->clean("43740999055");
Output:
437.409.990-55
Numbers.
numeric()
use FzPhpSanitize\Sanitize;
$value = Sanitize::numeric()->clean("asdfg123456");
Output:
123456
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
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
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
filter_var FILTER_SANITIZE_EMAIL
email()
use FzPhpSanitize\Sanitize;
$value = Sanitize::email()->clean("çótest@test.com");
Output:
test@test.com
Make a string lowercase.
strtolower()
use FzPhpSanitize\Sanitize;
$value = Sanitize::strtolower()->clean("FERNANDO ZUEET");
Output:
fernando zueet
Make a string uppercase.
strtoupper()
use FzPhpSanitize\Sanitize;
$value = Sanitize::strtoupper()->clean("fernando zueet");
Output:
FERNANDO ZUEET
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
Make a string's first character uppercase.
ucfirst()
use FzPhpSanitize\Sanitize;
$value = Sanitize::ucfirst()->clean("fernando zueet");
Output:
Fernando zueet
Make a string's first character lowercase.
lcfirst()
use FzPhpSanitize\Sanitize;
$value = Sanitize::lcfirst()->clean("Fernando zueet");
Output:
fernando zueet
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
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
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 format.
date(string $format = 'Y-m-d')
use FzPhpSanitize\Sanitize;
$value = Sanitize::date("Y-m-d")->clean("01/06/1987");
Output:
1987-06-01
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
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
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
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
Please see CONTRIBUTING for details.
If you discover security related issues, please email fernandozueet@hotmail.com instead of using the issue tracker.
The FZ Php Sanitize is licensed under the MIT license. See License File for more information.