- var_dump: documentation ( source)
- array_merge: documentation ( source)
- preg_split: documentation ( source)
<?php
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;
continue;
}
array_merge($tokens, preg_split('/\?/', $token, -1, PREG_SPLIT_DELIM_CAPTURE));
}
}
var_dump(getQueryTokens("SELECT * FROM foo WHERE 'a' = b AND f = ? AND q IN (?)"));