Trim

Trim - Strip whitespace (or other characters) from the beginning and end of a string.

Trim only works with string, any other type is ignored.

Basic usage

use Jawira\Sanitizer\Filters as Sanitizer;

class User {
  #[Sanitizer\Trim]
  public string $name;
}

Parameters

string characters (optional):
Set of characters you want to remove, default value is " \t\n\r\0\x0B".
string side (optional):
Use both to apply trim at the beginning and the end of string, this is the default value.
Use left to apply trim at the beginning of string.
Use right to apply trim at the end of string.

Examples

Remove spaces from the beginning and end of string:

use Jawira\Sanitizer\Filters as Sanitizer;

class User {
    #[Sanitizer\Trim]
    public string $name;
}
"Paul    " → "Paul"
"  Paul  " → "Paul"
"    Paul" → "Paul"
"\t\tPaul" → "Paul"
"Paul\r\n" → "Paul"

Remove spaces at the end of the string:

use Jawira\Sanitizer\Filters as Sanitizer;

class User {
    #[Sanitizer\Trim(side: 'right')]
    public string $name;
}
"Paul    " → "Paul"
"  Paul  " → "  Paul"
"    Paul" → "    Paul"
"\t\tPaul" → "\t\tPaul"
"Paul\r\n" → "Paul"

Remove plus and minus signs at the beginning of the string:

use Jawira\Sanitizer\Filters as Sanitizer;

class User {
    #[Sanitizer\Trim(side: 'left', characters: '+-')]
    public string $name;
}
"    Paul    " → "    Paul    "
"-+-+Paul+-+-" → "Paul+-+-"

See also

Pad - Pad a string to a certain length with another string.