我正在尝试格式化 sql 语句以使其更易于阅读。
$sql = "SELECT title, url, author, FROM_UNIXTIME(published, '%Y') AS year FROM articles WHERE id IN(2010,2011,2012)";
预期结果:
SELECT
title,
url,
author,
FROM_UNIXTIME(published, '%Y') AS year
FROM
articles
WHERE
id IN(2010, 2011, 2012)
它只是将 SELECT、FROM、WHERE... 等关键字放在自己的行上,
然后在每个逗号后插入换行符 + 制表符,但不是括号内的逗号。
这是我的尝试
$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
卡在这里:如何排除模式中括号内的逗号?