3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function get_phrases($sentence) { $inQuotes = false; $quoteChars = ['"', "'"]; $separators = [' ']; $phrase = null; $res = []; for ($i = 0; $i < strlen($sentence); $i++) { $t = $sentence[$i]; $isOpenQuote = !$inQuotes && in_array($t, $quoteChars); $isCloseQuote = $t == $inQuotes; $isLast = $i+1 == strlen($sentence); $isSeparator = !$inQuotes && in_array($t, $separators); if (!$inQuotes && $isOpenQuote) { // Opening quote $inQuotes = $t; continue; } elseif ($inQuotes && $isCloseQuote) { // Closing quote $inQuotes = false; } if (!$isOpenQuote && !$isCloseQuote && !$isSeparator) { $phrase .= $t; } if ($isSeparator || $isLast) { //yield $phrase; // or add to an array in pre-5.5 $res[] = $phrase; $phrase = null; } } return $res; } function assertEquals($expected, $actual) { if ($actual !== $expected) { echo sprintf("Assertion failed: %s does not equal %s\n", print_r($actual, true), print_r($expected, true) ); } else { echo "Assertion passed\n"; } } assertEquals(['Hello', 'world'], get_phrases('Hello world')); assertEquals(['I', 'am', 'a', 'foo bar'], get_phrases("I am a 'foo bar'"));
Output for 5.4.0 - 7.1.0
Assertion passed Assertion passed
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/adaDE on line 4
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/adaDE on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/adaDE on line 4
Process exited with code 255.