3v4l.org

run code in 300+ PHP versions simultaneously
<?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', array_fill(0, sizeof($words), ' '), $words ); } var_export(splitStrByWords($string));
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
array ( 0 => 'This is', 1 => 'my test', 2 => 'case for', 3 => 'an example', )

preferences:
66.95 ms | 406 KiB | 5 Q