3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function isQuotedString($token) { return in_array($token[0], array('"', "'")) && $token[0] == $token[strlen($token) - 1]; } function getQueryTokens($query) { static $expr = '/("(?:[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'(?:[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')/'; $tokens = []; foreach (preg_split($expr, $query, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY) as $token) { if (isQuotedString($token)) { $tokens[] = $token; } else { $tokens = array_merge($tokens, preg_split('/\?/', $token, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY)); } } return $tokens; } var_dump(getQueryTokens("SELECT * FROM foo WHERE 'a' = b AND f = ? AND q IN (?)"));
Output for 5.4.0 - 7.1.0
array(5) { [0]=> string(24) "SELECT * FROM foo WHERE " [1]=> string(3) "'a'" [2]=> string(13) " = b AND f = " [3]=> string(11) " AND q IN (" [4]=> string(1) ")" }
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/4F48B on line 11
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/4F48B on line 11
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/4F48B on line 11
Process exited with code 255.