Q:

How to extract words and phrases using preg_split() in PHP?

I need to extract the words and phrases within a text. For example, the text is:
Hello World, "Japan and China", Americans, Asians, "Jews and Christians", and semi-catholics, Jehovah's witnesses
Using preg_split(), it should return the following:

Hello
World
Japan and China
Americans
Asians
Jews and Christians
and
semi-catholics
Jehova's
witnesses

I need to know the RegEx for this to work (or is it possible?). Notice the rules, phrases are enclosed in quotes ("). Alphanumerics, single quotes (') and dashes (-) are considered part of the word (that's why "Jehova's" and "semi-catholics" are considered one word), the rest separated with spaces are considered as single words, while other symbols not mentioned are ignored

A:

You can actually do it very simply with str_getcsv like this:
// replace any comma or space by a singe space
$str = preg_replace('/(,+[ ]+)|([ ]+)/', ' ', $str);
// treat the input as CSV, the delimiters being spaces and enclusures double quotes
print_r(str_getcsv($str, ' ', '"'));

output:
Array
(
    [0] => Hello
    [1] => World
    [2] => Japan and China
    [3] => Americans
    [4] => Asians
    [5] => Jews and Christians
    [6] => and
    [7] => semi-catholics
    [8] => Jehovah's
    [9] => witnesses
)

