- array_map: documentation ( source)
- array_chunk: documentation ( source)
- var_export: documentation ( source)
- explode: documentation ( source)
- array_fill: documentation ( source)
<?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));