str_split().
Allows you to create a function which can be used to split into groups of specified chunk lengths. These can either be used as part of a Higher Order Function such as array_map() or as part of a compiled/pipe function.
/**
* @param int $length The length to split the string up with.
* @return Closure(string):array<string> The parts.
*/
Strings\splitByLength(int $length): ClosureWhen Strings\splitByLength() is called, it returns the following Closure which can be used like a regular function.
/**
* @param string $string The string to be split
* @return array<int, string>
* @psalm-pure
*/
$function(string $string): stringThis can be used to create a simple closure which can be used as a regular function.
// Create a closure which will take the first 2 characters of a string.
$splitIn2 = Strings\splitByLength(2);
// Called as a function.
echo $splitIn2('AABBCC'); // [AA, BB, CC]
// Used in a higher order function.
$array = array_map( $splitIn2, ['045678941212', '123456789012']);
print_r($array); // [[04,56,78,94,12,12], [12,34,56,78,90,12]]This can be called inline using currying.
echo Strings\splitByLength(2)('AABBCC'); // [AA, BB, CC]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\splitByLength(4), ['045678941212', '123456789012']);
print_r($array); // [[0456,7894,1212], [1234,5678,9012]]