class Philter

A PHP fluent input sanitiser.

Philter will sanitise any untrusted scalar value by chaining methods together, finally calling toString() toInt() or toFloat() to return the filtered input.

Methods

__construct(scalar $var, bool $raw = false)

The constructor requires the untrusted input.

bool|null
toBool()

Return the variable cast as a Boolean

float|null
toFloat(int|null $decimals = null)

Return the variable cast as a Float

int|null
toInt()

Return the variable cast as an Integer

string|null
toString()

Return the variable cast as a String

allow(string $allowed)

Removes any characters that are not in the allow list

alpha(string|null $allowed = null)

Removes any non alphabetical characters

alphanum(string|null $allowed = null)

Removes any non alpha-numeric characters

apply(Closure $closure)

Accepts a user defined closure

ascii()

Removes any non-ascii characters, transliterating as necessary

between(int $min, int $max)

Filters the variable, ensuring it is between $min and $max

contains(string $match, bool $match_case = false)

Checks that the variable contains the string in $match

cut(int|null $length)

Shortens the length to $len characters

default(mixed $default)

Sets a default value to be returned if the variable is null

digits(string|null $allowed = null)

Removes any non numeric characters

in(array $values, string $match_case = false)

Filter to check if the value is in the provided array of values

min(int $min)

Check the input is greater than the value of max, sets var to null if less than $Min

max(int $max)

Check the input is less than the value of max, sets var to null if greater than $max

numeric(string|null $allowed = null)

Removes any non numeric characters, allows typical currency markup + or - commas and decimals

trim(string|null $char = null)

Trims the leading and trailing characters from the variable

ltrim(string|null $char = null)

Trims the leftmost character matching $char

rtrim(string|null $char = null)

Trims the rightmost character matching $char

utf8(bool $ignore = false)

Convert the string to UTF-8 transliterating if possible

stripAttributes(string|null $allowed_tags = null)

Removes the majority of HTML tags leaving only a basic set without attributes

stripHtml()

Removes all HTML and javascript

stripJavascript()

Removes all javascript

stripTags(string|null $allowed_tags = null)

Removes the majority of HTML tags leaving only a basic set without attributes

stripUnprintable()

Removes any unprintable characters, this filter is processed by default

Details

at line 34
__construct(scalar $var, bool $raw = false)

The constructor requires the untrusted input.

Optionally a boolean to skip trimming and removing unprintable characters

Parameters

scalar $var
  • The variable to be filtered
bool $raw
  • If true Philter will not Automatically trim spaces and remove unprintable characters

at line 76
bool|null toBool()

Return the variable cast as a Boolean

Return Value

bool|null

at line 86
float|null toFloat(int|null $decimals = null)

Return the variable cast as a Float

Parameters

int|null $decimals
  • Limit the number of decimal places

Return Value

float|null

at line 99
int|null toInt()

Return the variable cast as an Integer

Return Value

int|null

at line 108
string|null toString()

Return the variable cast as a String

Return Value

string|null

at line 123
Philter allow(string $allowed)

Removes any characters that are not in the allow list

Parameters

string $allowed = A string containing the allowed characters

Return Value

Philter

at line 141
Philter alpha(string|null $allowed = null)

Removes any non alphabetical characters

Parameters

string|null $allowed = An optional string containing individual allowed characters

Return Value

Philter

at line 151
Philter alphanum(string|null $allowed = null)

Removes any non alpha-numeric characters

Parameters

string|null $allowed = An optional string containing individual allowed characters

Return Value

Philter

at line 161
Philter apply(Closure $closure)

Accepts a user defined closure

Parameters

Closure $closure
  • The user defined closure

Return Value

Philter

at line 171
Philter ascii()

Removes any non-ascii characters, transliterating as necessary

Return Value

Philter

at line 216
Philter between(int $min, int $max)

Filters the variable, ensuring it is between $min and $max

Parameters

int $min
int $max

Return Value

Philter

at line 234
Philter contains(string $match, bool $match_case = false)

Checks that the variable contains the string in $match

Parameters

string $match
  • The string to match
bool $match_case
  • Set true to match case

Return Value

Philter

at line 255
Philter cut(int|null $length)

Shortens the length to $len characters

Parameters

int|null $length
  • The number of characters to leave in the string

Return Value

Philter

at line 276
Philter default(mixed $default)

Sets a default value to be returned if the variable is null

Parameters

mixed $default

Return Value

Philter

at line 290
Philter digits(string|null $allowed = null)

Removes any non numeric characters

Parameters

string|null $allowed = A string containing individual allowed characters

Return Value

Philter

at line 301
Philter in(array $values, string $match_case = false)

Filter to check if the value is in the provided array of values

Parameters

array $values
  • The values to check
string $match_case
  • Compare values in a case sensitive

Return Value

Philter

at line 321
Philter min(int $min)

Check the input is greater than the value of max, sets var to null if less than $Min

Parameters

int $min
  • The lowest value allowed to pass through the filter

Return Value

Philter

at line 338
Philter max(int $max)

Check the input is less than the value of max, sets var to null if greater than $max

Parameters

int $max
  • The highest value allowed to pass through the filter

Return Value

Philter

at line 355
Philter numeric(string|null $allowed = null)

Removes any non numeric characters, allows typical currency markup + or - commas and decimals

Parameters

string|null $allowed = A string containing individual allowed characters

Return Value

Philter

at line 365
Philter trim(string|null $char = null)

Trims the leading and trailing characters from the variable

Parameters

string|null $char

Return Value

Philter

at line 383
Philter ltrim(string|null $char = null)

Trims the leftmost character matching $char

Parameters

string|null $char

Return Value

Philter

at line 401
Philter rtrim(string|null $char = null)

Trims the rightmost character matching $char

Parameters

string|null $char

Return Value

Philter

at line 419
Philter utf8(bool $ignore = false)

Convert the string to UTF-8 transliterating if possible

Parameters

bool $ignore
  • Discard unrepresentable charcters

Return Value

Philter

at line 445
Philter stripAttributes(string|null $allowed_tags = null)

Removes the majority of HTML tags leaving only a basic set without attributes

Parameters

string|null $allowed_tags
  • An optional set of tags to allow

Return Value

Philter

at line 470
Philter stripHtml()

Removes all HTML and javascript

Return Value

Philter

at line 482
Philter stripJavascript()

Removes all javascript

Return Value

Philter

at line 500
Philter stripTags(string|null $allowed_tags = null)

Removes the majority of HTML tags leaving only a basic set without attributes

Parameters

string|null $allowed_tags
  • An optional set of tags to allow

Return Value

Philter

at line 520
Philter stripUnprintable()

Removes any unprintable characters, this filter is processed by default

Return Value

Philter