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 converters

Type converters (integer, boolean, numeric, dateTime, json) have moved to Stage 1 (Converter) of the pipeline. They are documented in the Converters reference.


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']

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.