3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class SlightlyLessBollocksMySQLi { public function prepareWithParams($query, $params) { $tokens = $this->getQueryTokens($query); $query = ''; $args = ['']; $paramRefs = []; $i = 1; foreach ($tokens as $token) { if ($token !== '?') { $query .= $token; continue; } $paramRefs[$i] = array_shift($params); switch (true) { case is_array($paramRefs[$i]): $query .= implode(', ', array_fill(0, count($paramRefs[$i]), '?')); $tmp = $paramRefs[$i]; array_splice($paramRefs, $i, 1, $tmp); foreach ($tmp as $val) { switch (true) { case is_int($val): $args[0] .= 'i'; break; case is_float($val): $args[0] .= 'd'; break; default: $args[0] .= 's'; break; } } for ($j = $i; isset($paramRefs[$j]); $j++) { $args[] = &$paramRefs[$j]; } $i = $j; break; case is_int($paramRefs[$i]): case is_bool($paramRefs[$i]): $args[0] .= 'i'; $query .= '?'; $paramRefs[$i] = (int)$paramRefs[$i]; $args[] = &$paramRefs[$i++]; break; case is_float($paramRefs[$i]): $args[0] .= 'd'; $query .= '?'; $args[] = &$paramRefs[$i++]; break; default: $args[0] .= 's'; $query .= '?'; $paramRefs[$i] = (string)$paramRefs[$i]; $args[$i] = &$paramRefs[$i++]; break; } } var_dump($query, $args); } /** * @param string $token * @return bool */ private function isQuotedString($token) { return in_array($token[0], array('"', "'")) && $token[0] == $token[strlen($token) - 1]; } /** * "Tokenize" the query * * Actually all this does is split the query up into things that are and are not quoted strings, to avoid treating * a literal question mark as a placeholder. * * @param string $query * @return string[] */ private function getQueryTokens($query) { static $expr = '/("(?:[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'(?:[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')/'; $tokens = []; foreach (preg_split($expr, $query, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY) as $token) { if ($this->isQuotedString($token)) { $tokens[] = $token; } else { $tokens = array_merge($tokens, preg_split('/(\?)/', $token, -1, PREG_SPLIT_DELIM_CAPTURE)); } } return $tokens; } } (new SlightlyLessBollocksMySQLi)->prepareWithParams("SELECT * FROM foo WHERE 'a' = b AND foo = ? AND bar IN(?)", [2, [1, 2, 12.345, "hello", true]]);
Output for 5.4.0 - 7.1.0
string(69) "SELECT * FROM foo WHERE 'a' = b AND foo = ? AND bar IN(?, ?, ?, ?, ?)" array(7) { [0]=> string(6) "iiidss" [1]=> &int(2) [2]=> &int(1) [3]=> &int(2) [4]=> &float(12.345) [5]=> &string(5) "hello" [6]=> &bool(true) }
Output for 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/tdIqv on line 9
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/tdIqv on line 9
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/tdIqv on line 5
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/tdIqv on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/tdIqv on line 5
Process exited with code 255.