Skip to content

Filters reference

All filters are in the Verja\Filter\ namespace. In string definitions, use the class name with the first letter lowercase: 'trim', 'integer', 'convertCase:upper'.

Short syntax with IDE autocompletion

Just like validators, filters support a short static syntax via __callStatic:

use Verja\Filter as f;

$gate->accepts([
    'username' => [f::trim(), f::convertCase('lower')],
    'price'    => [f::numeric()],
]);

For custom filters, extend and annotate:

use Verja\Filter;

/**
 * @method static \App\Filter\Slugify slugify()
 */
class f extends Filter {}

Filters run before validators and transform the value. If a filter cannot process the value (wrong type, invalid format), it throws Verja\Exception\InvalidValue, which short-circuits the pipeline and adds an error — no further filters or validators run.


String filters

Trim

'trim', 'trim:characters', f::trim($characterMask = " \t\n\r\0\x0B"),
new Filter\Trim($characterMask = " \t\n\r\0\x0B")

Trims $characterMask from both ends of the string. Defaults to PHP's standard whitespace characters.

'trim'      // trim whitespace
'trim:/'    // trim slashes
'trim:/ '   // trim slashes and whitespace

ConvertCase

'convertCase:upper', f::convertCase($mode),
new Filter\ConvertCase($mode)

Converts the string case using mb_convert_case(). Non-string values pass through unchanged. $mode must be one of: - 'upper' — converts to uppercase - 'lower' — converts to lowercase - 'title' — converts to title case

Escape

'escape', f::escape(), new Filter\Escape()

Escapes special HTML characters using htmlspecialchars(). Useful when values are rendered directly into HTML.

Replace

'replace:search:replacement', f::replace($search, $replace),
new Filter\Replace($search, $replace)

Replaces all occurrences of $search with $replace in the string.

PregReplace

'pregReplace:pattern:replacement', f::pregReplace($pattern, $replace),
new Filter\PregReplace($pattern, $replace)

Replaces matches of the PCRE pattern $pattern with $replace. $replace can be a replacement string or a callable (maps to preg_replace_callback).

StripTags

'stripTags', 'stripTags:<p><strong>', f::stripTags($allowedTags = null),
new Filter\StripTags($allowedTags = null)

Strips HTML tags from strings using strip_tags(). Pass allowed tags to preserve specific elements. Non-string values pass through unchanged.

f::stripTags()              // '<p>hello</p>' → 'hello'
f::stripTags('<p><strong>') // keeps <p> and <strong>

Type-casting filters

These filters validate the input type before casting and throw InvalidValue if the value cannot be converted. This makes them safe to combine with validators that expect a specific type.

Integer

'integer', f::integer(), new Filter\Integer()

Converts the value to an integer. Accepts integer values and numeric strings. Throws if the value is not numeric. After this filter, downstream validators always see a PHP int.

->int('age', 'between:0:150')
// 'int()' applies Filter\Integer, then Between checks the integer value

Numeric

'numeric', f::numeric($decimalPoint = '.'),
new Filter\Numeric($decimalPoint = '.')

Converts the value to a float or integer (integer if no decimal part). Accepts numeric strings. Pass $decimalPoint to support alternative decimal separators (e.g. ',' for European locales). Throws if the value is not numeric.

Boolean

'boolean', f::boolean(),
new Filter\Boolean($stringTrue = [], $stringFalse = [], $overwrite = false)

Converts the value to a PHP boolean. Accepts bool, int, and the following strings by default:

  • truthy: '1', 'true', 't', 'yes', 'y'
  • falsy: '0', 'false', 'f', 'no', 'n'

Pass $stringTrue / $stringFalse to add extra accepted representations. Pass $overwrite = true to replace the defaults entirely. Throws InvalidValue for any other value.

->boolean('active')
// '1' → true, 'false' → false, 'maybe' → InvalidValue

DateTime

'dateTime', f::dateTime($timeZone = null, $format = null, $strict = false),
new Filter\DateTime($timeZone = null, $format = null, $strict = false)

Converts a string to a PHP \DateTime object. Without $format, parses any string accepted by PHP's DateTime constructor. With $format, requires the value to match that format (delegates to DateTime::createFromFormat()). $strict = true additionally rejects overflow dates (e.g. 2025-01-32). $timeZone can be a \DateTimeZone instance, a timezone name string, or a UTC offset integer.

Throws InvalidValue if the value does not parse.

->date('published_at')
// '2024-03-15' → DateTime object (no format — accepts any parseable date)

f::dateTime(null, 'Y-m-d')
// '2024-03-15' → DateTime, '15.03.2024' → InvalidValue

Array filters

ArrayFilter

'arrayFilter', f::arrayFilter($callback = null),
new Filter\ArrayFilter($callback = null)

Removes elements from arrays. Non-array values pass through unchanged. By default removes null and empty string ''. Pass a callback to define custom keep logic:

f::arrayFilter()                          // remove null and ''
f::arrayFilter(fn($v) => $v !== null)     // remove only null
f::arrayFilter(fn($v) => is_int($v))      // keep only integers

Split

f::split($delimiter, $trim = true),
new Filter\Split($delimiter, $trim = true)

Splits a string into an array by $delimiter. Trims whitespace from each element by default. Non-string values pass through unchanged.

f::split(',')         // 'a, b , c' → ['a', 'b', 'c']
f::split(',', false)  // 'a, b , c' → ['a', ' b ', ' c']

Utility filters

DefaultValue

f::defaultValue($default),
new Filter\DefaultValue($default)

Returns $default when the value is null, passes everything else through unchanged. Useful after Nullable to provide a fallback:

->any('role', 'nullable', f::defaultValue('guest'))
// null or '' → 'guest', 'admin' → 'admin'

Other

Callback

new Filter\Callback($callback)

Delegates to a custom callable. The callable receives ($value, $context) and returns the transformed value. Cannot be used as a string definition. See custom validators and filters.