<?php
$string = "This is my test case for an example";
function splitStrByWords($sentence, $wordCount=2) {
$words = array_chunk(
explode(' ', $sentence),
$wordCount
);
return array_map(
'implode',
$words,
array_fill(0, sizeof($words), ' ')
);
}
var_export(splitStrByWords($string));
Fatal error: Uncaught TypeError: implode(): Argument #2 ($array) must be of type ?array, string given in /in/RKFkV:10
Stack trace:
#0 [internal function]: implode(Array, ' ')
#1 /in/RKFkV(10): array_map('implode', Array, Array)
#2 /in/RKFkV(16): splitStrByWords('This is my test...')
#3 {main}
thrown in /in/RKFkV on line 10
Process exited with code 255.
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33
Fatal error: Uncaught TypeError: implode(): Argument #2 ($array) must be of type ?array, string given in /in/RKFkV:13
Stack trace:
#0 [internal function]: implode(Array, ' ')
#1 /in/RKFkV(13): array_map('implode', Array, Array)
#2 /in/RKFkV(16): splitStrByWords('This is my test...')
#3 {main}
thrown in /in/RKFkV on line 13
Process exited with code 255.