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/ */
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çù");