4.7.5. Rules To Sanitize Fields

4.7.5.1. alnum

Sanitizes to leave only alphanumeric characters.

$filter->sanitize('field')->to('alnum');

4.7.5.2. alpha

Sanitizes to leave only alphabetic characters.

$filter->sanitize('field')->to('alpha');

4.7.5.3. between

Sanitizes so that values lower than the range are forced up to the minimum, and values higher than the range are forced down to the maximum.

$filter->sanitize('field')->to('between', $min, $max);

4.7.5.4. bool

Sanitizes to a strict PHP boolean value. Pseudo-true values include the strings '1', 'y', 'yes', and 'true'; pseudo-false values include the strings '0', 'n', 'no', and 'false'.

// sanitize to `true` and `false`
$filter->sanitize('field')->to('bool');

You can sanitize to alternative true and false values in place of PHP true and false.

// sanitize to alternative true and false values
$filter->sanitize('field')->to('bool', $value_if_true, $value_if_false);

4.7.5.5. callback

Sanitizes the value using a callable/callback. The callback should take two arguments, $subject and $field, to indicate the subject and the field within that subject. It should return true to pass, or false to fail.

$filter->sanitize('field')->to('callback', function ($subject, $field) {
    // always force the field to 'foo'
    $subject->$field = 'foo';
    return true;
});

N.b.: Always use object notation ($subject->$field) and not array notation ($subject[$field]) in the callable, as the Filter converts arrays to objects on the fly.

4.7.5.6. dateTime

Sanitizes the value to a specified date/time format, default 'Y-m-d H:i:s'.

$filter->sanitize('field')->to('dateTime', $format);

4.7.5.7. field

Sanitizes to the value of another field in the subject.

$filter->sanitize('field')->to('field', 'other_field_name');

4.7.5.8. float

Sanitizes the value to transform it into a float; for weird strings, this may not be what you expect.

$filter->sanitize('field')->to('float');

4.7.5.9. int

Sanitizes the value to transform it into an integer; for weird strings, this may not be what you expect.

$filter->sanitize('field')->to('int');

4.7.5.10. isbn

Sanitizes the value to an ISBN (International Standard Book Number).

$filter->sanitize('field')->to('isbn');

4.7.5.11. lowercase

Sanitizes the value to all lowercase characters.

$filter->sanitize('field')->to('lowercase');

4.7.5.12. lowercaseFirst

Sanitizes the value to begin with a lowercase character.

$filter->sanitize('field')->to('lowercaseFirst');

4.7.5.13. max

Sanitizes so that values higher than the maximum are forced down to the maximum.

$filter->sanitize('field')->to('max', $max);

4.7.5.14. min

Sanitizes so that values lower than the minimum are forced up to the minimum.

$filter->sanitize('field')->to('min', $min);

4.7.5.15. now

Sanitizes the value to force it to the current datetime, default format 'Y-m-d H:i:s'.

$filter->sanitize('field')->to('now', $format);

4.7.5.16. remove

Removes the field from the subject with unset().

$filter->sanitize('field')->to('remove');

4.7.5.17. regex

Sanitizes the value using preg_replace().

$filter->sanitize('field')->to('regex', $expr, $replace);

4.7.5.18. string

Sanitizes the value by casting to a string and optionally using str_replace() to find and replace within the string.

$filter->sanitize('field')->to('string', $find, $replace);

4.7.5.19. strlen

Sanitizes the value to cut off longer values at the right, and str_pad() shorter ones.

$filter->sanitize('field')->to('strlen', $len[, $pad_string[, $pad_type]]);

4.7.5.20. strlenBetween

Sanitizes the value to truncate values longer than the maximum, and str_pad() shorter ones.

$filter->sanitize('field')->to('strlenBetween', $min, $max[, $pad_string[, $pad_type]]);

4.7.5.21. strlenMax

Sanitizes the value to truncate values longer than the maximum.

$filter->sanitize('field')->to('strlenMax', $max);

4.7.5.22. strlenMin

Sanitizes the value to str_pad() values shorter than the minimum.

$filter->sanitize('field')->to('strlenMin', $min[, $pad_string[, $pad_type]]);

4.7.5.23. titlecase

Sanitizes the value to titlecase (eg. "Title Case").

$filter->sanitize('field')->to('titlecase');

4.7.5.24. trim

Sanitizes the value to trim() it. Optionally specify characters to trim.

$filter->sanitize('field')->to('trim', $chars);

4.7.5.25. uppercase

Sanitizes the value to all uppercase characters.

$filter->sanitize('field')->to('uppercase');

4.7.5.26. uppercaseFirst

Sanitizes the value to begin with an uppercase character.

$filter->sanitize('field')->to('uppercaseFirst');

4.7.5.27. value

Sanitizes to the specified value.

$filter->sanitize('field')->to('value', $other_value);

4.7.5.28. word

Sanitizes the value to remove non-word characters.

$filter->sanitize('field')->to('word');