PHP:
$query = 'SELECT column1, column2 as foo, table.column3, table.column4 as foo,
`column5`, `column6` as foo, `table`.`column7`, `table`.`column8` as foo
FROM table';
$query = preg_replace('/^SELECT(.*?)FROM.*$/s', '$1', $query); // To remove the "SELECT" and "FROM table..." parts
preg_match_all('/(?:
(?:`?\w+`?\.)? (?:`)?(\w+)(?:`)? (?:\s*as\s*\w+)?\s*
)+/x',$query, $m);
print_r($m[1]);
:
Array
(
[0] => column1
[1] => column2
[2] => column3
[3] => column4
[4] => column5
[5] => column6
[6] => column7
[7] => column8
)
Live demo: http://www.rubular.com/r/H960NFKCTr
UPDATE:. "", SQL (: #__tracktime_projects), . , , , , , i, :
$query = 'SELECT column1, column2 as foo, table.column3, table.column4 as foo,
`column5`, `column6` as foo, `table`.`column7`, `table`.`column8` as foo, `#__tracktime_projects`.`pr_name` AS project_name, `#wut`
FROM table';
$query = preg_replace('/^SELECT(.*?)FROM.*$/s', '$1', $query); // To remove the "SELECT" and "FROM table..." parts
$allowed = '\w
preg_match_all('/(?:
(?:`?['.$allowed.']++`?\.)?
# ^--------TableName--------^
(?:`)?(['.$allowed.']++)(?:`)?
# ^----------ColumnName--------^
(?:\s*as\s*['.$allowed.']++)?\s*
# ^-------------AsFoo------------^
)+
/xi',$query, $m);
print_r($m[1]);
:
Array
(
[0] => column1
[1] => column2
[2] => column3
[3] => column4
[4] => column5
[5] => column6
[6] => column7
[7] => column8
[8] => pr_name
[9] =>
)
Live demo: http://www.rubular.com/r/D0iIHJQwB8