Strings\splitPattern()

transformer returns Closure pure
string → (string → string[])
At a glance — Wraps preg_split().

Returns a function which can be used to to split a string into an array of strings. The created function can then reused over any string, or used as part of a Higher Order Function such as array_map().

/**
  * @param string $pattern The pattern to look for.
  * @return Closure(string):string[]
  */
 Strings\splitPattern(string $pattern): Closure

Returned Closure

When Strings\splitPattern() is called, it returns the following Closure which can be used like a regular function.

/**
  * @param string $source
  * @return string[]
  * @psalm-pure
  */ 
$function(string $source): array

Examples

Partial Application

This can be used to create a simple closure which can be used as a regular function.

// Create function to split an array of strings using - as a delimiter.
$splitOnDashes = Strings\splitPattern('/-/');

// Called as a function.
$splitOnDashes('its-foo'); // ['its', 'foo']

// Used in a higher order function.
$array = array_map($splitOnDashes, ['its-foo', 'its-bar']);
print_r($array); // [['its', 'foo'], ['its', 'bar']]

Curried

This can be called inline using currying.

Strings\splitPattern('/-/')('its-foo');  // ['its', 'foo']

Inlined with Higher Order Function

If you are not planning on reusing the Closure created, you can just call it inline with a higher order function as its callable.

$array = array_map(Strings\splitPattern('/-/'), ['its-foo', 'its-bar']);

print_r($array); // [['its', 'foo'], ['its', 'bar']]

Details

Strings Functions

Releated String transform Functions