https://github.com/bayfrontmedia/php-sanitize
Simple class used to sanitize, filter and cast data.
https://github.com/bayfrontmedia/php-sanitize
cast escape filter php sanitize xss
Last synced: 16 days ago
JSON representation
Simple class used to sanitize, filter and cast data.
- Host: GitHub
- URL: https://github.com/bayfrontmedia/php-sanitize
- Owner: bayfrontmedia
- License: mit
- Created: 2020-07-27T19:13:36.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-12-23T20:26:24.000Z (4 months ago)
- Last Synced: 2025-03-25T16:51:29.374Z (about 1 month ago)
- Topics: cast, escape, filter, php, sanitize, xss
- Language: PHP
- Homepage:
- Size: 16.6 KB
- Stars: 4
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
## PHP sanitize
Simple class used to sanitize, filter and cast data.
- [License](#license)
- [Author](#author)
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)## License
This project is open source and available under the [MIT License](LICENSE).
## Author
- [Bayfront Media homepage](https://www.bayfrontmedia.com?utm_source=github&utm_medium=direct)
- [Bayfront Media GitHub](https://github.com/bayfrontmedia)## Requirements
* PHP `^8.0` (Tested up to `8.4`)
## Installation
```
composer require bayfrontmedia/php-sanitize
```## Usage
- [cast](#cast)
- [email](#email)
- [url](#url)
- [path](#path)
- [escape](#escape)
### cast
**Description:**
Ensures a given variable will be returned as a specific type. Defaults to "string".
**Parameters:**
- `$var` (mixed)
- `$type = self::CAST_STRING` (string): Any `CAST_*` constantValid cast types are available as the following constants:
- `CAST_ARRAY`: array
- `CAST_BOOL`: bool
- `CAST_FLOAT`: float
- `CAST_INT`: int
- `CAST_OBJECT`: object
- `CAST_STRING`: string**Returns:**
- (mixed)
**Example:**
```
use Bayfront\Sanitize\Sanitize;$input = '1.42';
echo Sanitize::cast($input, Sanitize::CAST_FLOAT);
```
**Description:**
Filters string for valid email characters.
**Parameters:**
- `$email` (string)
**Returns:**
- (string)
**Example:**
```
use Bayfront\Sanitize\Sanitize;echo Sanitize::email('[email protected]');
```
### url
**Description:**
Filters string for valid URL characters.
**Parameters:**
- `$url` (string)
**Returns:**
- (string)
**Example:**
```
use Bayfront\Sanitize\Sanitize;echo Sanitize::url('https://www.example.com);
```
### path
**Description:**
Filters string for valid path syntax, with optional trailing slash.
This method removes any whitespace, spaces, and leading slashes. It will also convert reverse and multiple slashes to one single forward slash.
**Parameters:**
- `$path` (string)
- `$trailing = true` (bool): Require trailing slash**Returns:**
- (string)
**Example:**
```
use Bayfront\Sanitize\Sanitize;$path = '/some/ bad//path';
echo Sanitize::path($path);
```
### escape
**Description:**
Escape strings and arrays. Other data types return their original value.
**NOTE:** Use caution when escaping entire arrays, as strings should typically only be escaped when outputting to HTML.
See: [https://www.php.net/manual/en/mbstring.supported-encodings.php](https://www.php.net/manual/en/mbstring.supported-encodings.php)
**Parameters:**
- `$value` (mixed)
- `$encoding = 'UTF-8'` (string)**Returns:**
- (mixed)
**Example:**
```
use Bayfront\Sanitize\Sanitize;$html = 'Hyperlink';
echo Sanitize::escape($html);
```