3v4l.org

run code in 300+ PHP versions simultaneously
<?php $string = "This is a string"; $words = []; $spaces = []; foreach (preg_split('~( +)~', $string, -1, 3) as $s) { if (ctype_space($s)) { $spaces[] = strlen($s); } else { $words[] = $s; } } var_export([ 'words' => $words, 'spaces' => $spaces ]);

preferences:
55.83 ms | 406 KiB | 5 Q