Using Php. Need:
On String Input: "Some terms with spaces between"
OutputArray <String>: {Some, terms, with, spaces, between}
Using Php. Need:
On String Input: "Some terms with spaces between"
OutputArray <String>: {Some, terms, with, spaces, between}
You could use explode
, split
or preg_split
.
explode
uses a fixed string:
$parts = explode(' ', $string);
while split
and preg_split
use a regular expression:
$parts = split(' +', $string);
$parts = preg_split('/ +/', $string);
An example where the regular expression based splitting is useful:
$string = 'foo bar'; // multiple spaces
var_dump(explode(' ', $string));
var_dump(split(' +', $string));
var_dump(preg_split('/ +/', $string));
Just a question, but are you trying to make json out of the data? If so, then you might consider something like this:
return json_encode(explode(' ', $inputString));
print_r(str_word_count("this is a sentence", 1));
Results in:
Array ( [0] => this [1] => is [2] => a [3] => sentence )