trim

Remove leading and trailing characters from string

Definition

string trim(string $string, array[string] $charactersToRemove)

Removes $charactersToRemove from start and end of $string.

Parameters

string $string

The string to remove characters from.

array[string] $charactersToRemove

An array containing the characters to remove from the string.

If left empty, all whitespace characters are removed.

Returns

string

$string with the characters removed.

Examples

{
  a: trim('abcMyStringabc', ['a']),
  b: trim('abcMyStringabc', ['a','b']),
  c: trim('abcMyStringabc', ['a','b','c']),
  d: trim('_I have a string', ['_']),
  e: trim('I have a string_', ['_']),
  f: trim('No need to trim', []),
  g: trim(null, [])
}
{
  "a": "bcMyStringabc",
  "b": "cMyStringabc",
  "c": "MyString",
  "d": "I have a string",
  "e": "I have a string",
  "f": "No need to trim",
  "g": null
}