Often times I find myself having to handle a particular portion
of a SQL statement via a script. I've written a lot of
specialized regular expressions over time to handle these tasks,
but the one that I've had to write the most is a basic
'tokenizer' which understands the quoting semantics of a MySQL
statement. That is, it understands `BackTick`, 'Single Quote' and
"Double Quoted" strings.
#PHP Heredoc syntax $regex = <<< END_OF_REGEX / [^ \"'`(),]*\([^)]*\) #match functions like concat(x,"y",`a`.`z`) or sum(`xyz`); |\([^)]*?\) #match grouped items |"[^"]*?" #match double quoted items |'[^']*?' #match …[Read more...]