<?php $string = "This is a string"; $words = []; $spaces = []; foreach (preg_split('~( +)~', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $s) { if (ctype_space($s)) { $spaces[] = strlen($s); } else { $words[] = $s; } } var_export([ 'words' => $words, 'spaces' => $spaces ]);
You have javascript disabled. You will not be able to edit any code.