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?
If you can have an arbitrary number of nested parentheses, you are best off doing it manually within the callback function (by walking the string character-by-character and counting parentheses). If you can assure that you only have one layer of parentheses and all parentheses are matched (so that you have a valid syntax), you could use a lookahead, that asserts that there is no )
until the next (
or the end of the string:
'/,(?=[^)]*(?:[(]|$))/'
You do not even need the callback now. Just use preg_replace
.
The problem still is that this will also take into account parentheses within strings in your SQL statement, which is why this problem is generally a bit too tough for regular expressions and can only be solved with them if you impose said conditions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With