An open API service indexing awesome lists of open source software.

https://github.com/truongwp/sanitizer

A simple and stand-alone PHP sanitizer library with no dependencies.
https://github.com/truongwp/sanitizer

php php-library sanitization sanitizer

Last synced: 6 months ago
JSON representation

A simple and stand-alone PHP sanitizer library with no dependencies.

Awesome Lists containing this project

README

          

## Sanitizer

**Sanitizer** is a simple and stand-alone PHP sanitizer library with no dependencies.

### Installation
Uses Composer to install and update:
```
composer require "truongwp/sanitizer=*"
```

**Sanitizer** require PHP >= 5.3

### Usage

```php
' Foo bar ',
'age' => ' 24 ',
);

$rules = array(
'name' => 'trim|strtolower|ucwords',
'age' => 'intval',
);

$output = $sanitizer->sanitize($input, $rules);
```

The `$output`:

```php
array(
'name' => 'Foo Bar',
'age' => 24,
);
```

Multiple rules can be passed as string delimiter by `|` or use an array:
```php
array('trim', 'strtolower', 'ucwords'),
'age' => 'intval',
);
```

By default, rule name is PHP function. So you can easily add a custom function to sanitize.
```php
'//foo',
);
$rules = array(
'name' => 'trim_slasses',
);
$output = $sanitizer->sanitize($input, $rules);
```

The result:
```php
array(
'name' => 'foo',
)
```

If you want to pass additional parameters to sanitizer function, you can append them to rule name and delimited by `:`.
```php
'foo',
);
$rules = array(
'name' => 'prefix_suffix:prefix_:_suffix',
);
$output = $sanitizer->sanitize($input, $rules);
```

The result:
```php
array(
'name' => 'prefix_foo_suffix',
)
```

You can also add custom sanitizer class by using SanitizerRegistry.
```php
'05/30/2017',
);
$rules = array(
'name' => 'date_format:Y-m-d',
);
$output = $sanitizer->sanitize($input, $rules);
```

The result:
```php
array(
'day' => '2017-05-30',
)
```

### Contributing
Contributor: [@truongwp](https://truongwp.com)

Bug reports or Pull requests are welcome.