- var_export: documentation ( source)
- ctype_space: documentation ( source)
- preg_split: documentation ( source)
<?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
]);