Your sample input is relatively calm. If you are trusting your users to perfectly enter qualifying substrings which are always delimited by exactly one space, then the best choice is explode()
.
If the user input can vary beyond just lowercase letters or you want to validate while extracting, there are other more appropriate functions to call.
Understanding you business logic will determine the best solution for this task.
A demonstration:
$input = "foo bar, php js double-space apo'strophe 9 ";
echo 'explode(): '; var_export(explode(' ', $input));
echo "\npreg_split(): "; var_export(preg_split('/ +/', $input, null, PREG_SPLIT_NO_EMPTY));
echo "\nstr_word_count(): "; var_export(str_word_count($input, 1));
echo "\npreg_match_all(): "; var_export(preg_match_all('/[a-z]+/', $input, $output) ? $output[0]: []);
Output:
explode(): array (
0 => 'foo',
1 => 'bar,',
2 => 'php',
3 => 'js',
4 => '',
5 => 'double-space',
6 => 'apo\'strophe',
7 => '9',
8 => '',
)
preg_split(): array (
0 => 'foo',
1 => 'bar,',
2 => 'php',
3 => 'js',
4 => 'double-space',
5 => 'apo\'strophe',
6 => '9',
)
str_word_count(): array (
0 => 'foo',
1 => 'bar',
2 => 'php',
3 => 'js',
4 => 'double-space',
5 => 'apo\'strophe',
)
preg_match_all(): array (
0 => 'foo',
1 => 'bar',
2 => 'php',
3 => 'js',
4 => 'double',
5 => 'space',
6 => 'apo',
7 => 'strophe',
)