A sentence can be parsed in many different ways in PHP. A few methods are presented here along with their analysis. Regardless of the language you use for parsing a sentence, you can either match the characters of interest, or match the characters you wish to exclude and split on them or you can use a ready-made function if one is available.
Consider the following code:
$sentence = "Hey, Carol's 18th birthday is 15 days from today.";
$words = preg_split('/\W+/', $sentence);
print_r($words);
Output:
Array
(
[0] => Hey
[1] => Carol
[2] => s
[3] => 18th
[4] => birthday
[5] => is
[6] => 15
[7] => days
[8] => from
[9] => today
[10] =>
)
The first example uses \W which matches non-word characters. Non word characters include apostrophe (‘), comma (,) and period (.) Carol’s is split into two words. Also note the empty cell at the end. Empty cells can be removed with PREG_SPLIT_NO_EMPTY argument as follows:
$sentence = "Hey, Carol's 18th birthday is 15 days from today.";
$words = preg_split('/\W+/', $sentence, -1, PREG_SPLIT_NO_EMPTY);
print_r($words);
Output:
Array
(
[0] => Hey
[1] => Carol
[2] => s
[3] => 18th
[4] => birthday
[5] => is
[6] => 15
[7] => days
[8] => from
[9] => today
)
Consider this code:
print "\nExample 2\n";
$words = preg_split("/[^\w']+/", $sentence);
print_r($words);
print "\nExample 3\n";
$words = preg_split("/[\s.,]+/", $sentence);
print_r($words);
print "\nExample 5\n";
$words = preg_split("/[^\w']+/", $sentence, -1, PREG_SPLIT_NO_EMPTY);
print_r($words);
print "\nExample 6\n";
$words = preg_split("/[\s.,]+/", $sentence, -1, PREG_SPLIT_NO_EMPTY);
print_r($words);
print "\nExample 7\n";
$words = str_word_count($sentence, 1, '0123456789');
print_r($words);
print "\nExample 8\n";
$words = preg_split('#[\\s.,]#', $sentence, -1, PREG_SPLIT_NO_EMPTY);
print_r($words);
Output
Example 2
Array
(
[0] => Hey
[1] => Carol's
[2] => 18th
[3] => birthday
[4] => is
[5] => 15
[6] => days
[7] => from
[8] => today
[9] =>
)
Example 3
Array
(
[0] => Hey
[1] => Carol's
[2] => 18th
[3] => birthday
[4] => is
[5] => 15
[6] => days
[7] => from
[8] => today
[9] =>
)
Example 4
Example 5
Array
(
[0] => Hey
[1] => Carol's
[2] => 18th
[3] => birthday
[4] => is
[5] => 15
[6] => days
[7] => from
[8] => today
)
Example 6
Array
(
[0] => Hey
[1] => Carol's
[2] => 18th
[3] => birthday
[4] => is
[5] => 15
[6] => days
[7] => from
[8] => today
)
Example 7
Array
(
[0] => Hey
[1] => Carol's
[2] => 18th
[3] => birthday
[4] => is
[5] => 15
[6] => days
[7] => from
[8] => today
)
Example 8
Array
(
[0] => Hey
[1] => Carol's
[2] => 18th
[3] => birthday
[4] => is
[5] => 15
[6] => days
[7] => from
[8] => today
)