I'm trying to format a sql statement to make it easier to read.
$sql = "SELECT title, url, author, FROM_UNIXTIME(published, '%Y') AS year FROM articles WHERE id IN(2010,2011,2012)";
expected result:
SELECT
title,
url,
author,
FROM_UNIXTIME(published, '%Y') AS year
FROM
articles
WHERE
id IN(2010, 2011, 2012)
It simply makes those keywords like SELECT, FROM, WHERE... on their own lines,
and then insert a newline + tab after each comma, but, NOT those commas inside parentheses.
here is my attemp
$sql = preg_replace('/(SELECT|FROM[^_]|WHERE)/', "\n$1\n\t", $sql);
function replace_commas($matches) {
return str_replace(",", ",\n\t", $matches[0]);
}
$sql = preg_replace_callback("/(.*)/s", 'replace_commas', $sql);
// ^^^^^^^
// stuck here
stuck here: how to exclude those commas inside parentheses in the pattern?