3v4l.org

run code in 300+ PHP versions simultaneously
<?php function get_phrases($sentence) { $inQuotes = false; $quoteChars = ['"', "'"]; $separators = [' ', "\n"]; $phrase = null; foreach ((string) $sentence as $i => $t) { if (!$inQuotes && in_array($t, $quoteChars)) { // Opening quote $inQuotes = $t; } elseif ($inQuotes && $t == $inQuotes) { // Closing quote $inQuotes = false; } elseif (!$inQuotes && in_array($t, $separators)) { yield $phrase; // or add to an array in pre-5.5 $phrase = null; } else { $phrase .= $t; } } } var_dump(get_phrases('Hello world')); var_dump(get_phrases("I am a 'foo bar'"));

preferences:
57.31 ms | 402 KiB | 5 Q