clsystems/php-filter

Php Input Filter package

v1.0.0 2020-06-14 22:42 UTC

This package is auto-updated.

Last update: 2024-10-29 05:53:36 UTC


README

Installation via Composer

{
	"require": {
		"clsystems/php-filter": "~1.0"
	}
}

Alternatively, from the command line:

composer require clsystems/php-filter

Usage

use CLSystems\Php\Filter;

// Syntax
Filter::clean($source, $type);

// Example
$source = '<h1>Hello World!</h1>';

// Return 'Hello World!'
$filtered = Filter::clean($source, 'string');

// Source array
$source = [
	'<h1>Hello World!</h1>',
	'<h1>Hello VietNam!</h1>',
];

// Return ['Hello World!', 'Hello VietNam!']
$filtered = Filter::clean($source, 'string');

// Multi-type
$source = '  <h1>Hello World!</h1>  ';

// Return 'Hello World!'
$filtered = Filter::clean($source, ['string', 'trim']);

Filter types

  • int
  • uint (unsigned int)
  • float
  • ufloat (unsigned float)
  • boolean
  • alphaNum (alpha numeric string)
  • base64
  • string (no HTML tags)
  • email
  • url
  • slug (url alias without slash)
  • path (url alias with slash)
  • unset (return NULL value)
  • jsonEncode
  • jsonDecode
  • yesNo (return 'Y' or 'N')
  • inputName (regex /[^a-zA-Z0-9_]/)
  • unique (array unique)
  • basicHtml

Fallback default

if (function_exists($type))
{
	if (is_array($value))
	{
		$result = array_map($type, $value);
	}
	else
	{
		$result = $type($value);
	}
}
else
{
	$result = $value;
}