$str = \"This is a string\"; $words = explode(\" \", $str);
Works fine, but spaces still go into array:
$words === array (\'This
You can use preg_split() for the first array:
preg_split()
$str = 'This is a string'; $words = preg_split('#\s+#', $str);
And preg_match_all() for the $spaces array:
preg_match_all()
$spaces
preg_match_all('#\s+#', $str, $m); $spaces = array_map('strlen', $m[0]);