hopefully this should be a quick and simple one, using PHP I\'m trying to split a string into an array but by only the last instance of whitespace. So far I have...
This should work:
$str="hello this is a space";
preg_match('~^(.*)\s+([^\s]+)$~', $str, $matches);
$result = array($matches[1], $matches[2]);
You could do it without a regex:
$parts = array_map('trim', explode(' ', $str));
$result = array(
implode(' ', array_slice($parts, 0, -1)),
end($parts)
);
or
$lastSpace = strrpos($str, ' ');
$str1 = trim(substr($str, 0, $lastSpace));
$str2 = trim(substr($str, $lastSpace));
$result = array( $str1, $str2 );