Split_on

The function split_on can be used to split a string into an array of strings where it is split on defined string separators.

Syntax

array split_on(string $input, boolean $ignore_empty, args[]:string $separators)

Splits the $input string into an array of strings where it is split on the given string $separators defined. $ignore_empty defines whether empty strings in the new array should be ignored or not.

Parameters

$input: The string that should be split.
$ignore_empty: The boolean that defines whether empty strings should be ignored in the array.
$separators: A variable number of strings that the $input string should be split on.

Example

{
    "InputString": "This-is--some-text"
}
{
    OutputIgnoreTrue: split_on(InputString, `true`, '-'),
    OutputIgnoreFalse: split_on(InputString, `false`, '-')
}
{
    "OutputIgnoreTrue": ["This", "is", "some", "text"],
    "OutputIgnoreFalse": ["This", "is", "", "some", "text"]
}

Above examples splits the $input string on the string separator “-“. The two outputs shows the difference between having $ignore_empty to true or false.
True removes the empty strings from the array where false keeps the empty strings in the array.

Notes

Multiple separators

As mentioned in the parameters section, the $separators parameter is a variable number of strings. This means that it is possible to define multiple separator strings that the $input string can be split on.

Example

{
    "MultipleInput": "This-is new-text"
}
{
    MultipleSeparators: split_on(MultipleInput, `true`, '-', ' ')
}
{
    "MultipleSeparators": ["This", "is", "new", "text"]
}

Above example shows how the MultipleInput string is split on both “-” and ” “.