bayfrontmedia/php-sanitize

Simple class used to sanitize, filter and cast data.

v2.0.0 2023-01-26 16:58 UTC

This package is auto-updated.

Last update: 2024-04-26 19:25:29 UTC


README

Simple class used to sanitize, filter and cast data.

License

This project is open source and available under the MIT License.

Author

Bayfront Media

Requirements

  • PHP ^8.0

Installation

composer require bayfrontmedia/php-sanitize

Usage

cast

Description:

Ensures a given variable will be returned as a specific type. Defaults to "string".

Parameters:

  • $var (mixed)
  • $type = 'string' (string)

Valid $type values are:

  • int
  • float
  • bool
  • object
  • array
  • string

Returns:

  • (mixed)

Example:

use Bayfront\Sanitize\Sanitize;

$input = '1.42';

echo Sanitize::cast($input, 'float');

email

Description:

Filters string for valid email characters.

Parameters:

  • $email (string)

Returns:

  • (string)

Example:

use Bayfront\Sanitize\Sanitize;

echo Sanitize::email('email@example.com');

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

Parameters:

  • $value (mixed)
  • $encoding = 'UTF-8' (string)

Returns:

  • (mixed)

Example:

use Bayfront\Sanitize\Sanitize;

$html = '<a href="#">Hyperlink</a>';

echo Sanitize::escape($html);