[TOC] >[github](https://github.com/jdorn/sql-formatter/) ## 安装 `composer require jdorn/sql-formatter` ## 使用 ### 格式化加高亮 ``` $query = "SELECT count(*),`Column1`,`Testing`, `Testing Three` FROM `Table1` WHERE Column1 = 'testing' AND ( (`Column2` = `Column3` OR Column4 >= NOW()) ) GROUP BY Column1 ORDER BY Column3 DESC LIMIT 5,10"; echo SqlFormatter::format($query); ``` ![UTOOLS1571379162552.png](https://i.loli.net/2019/10/18/2nJ9jzeYHom6Urt.png) ### 格式化不高亮 ``` echo SqlFormatter::format($query,false); ``` 命令行执行 ``` SELECT count(*), `Column1`, `Testing`, `Testing Three` FROM `Table1` WHERE Column1 = 'testing' AND ( ( `Column2` = `Column3` OR Column4 >= NOW() ) ) GROUP BY Column1 ORDER BY Column3 DESC LIMIT 5, 10 ``` ### 高亮不格式化 ``` echo SqlFormatter::highlight($query); ``` ![UTOOLS1571379420450.png](https://i.loli.net/2019/10/18/dIhS9orHGyeYwL2.png) ### 压缩 Query ``` $sql = "-- This is a comment SELECT /* This is another comment On more than one line */ Id #This is one final comment as temp, DateCreated as Created FROM MyTable;"; echo SqlFormatter::compress($sql); //SELECT Id as temp, DateCreated as Created FROM MyTable; ``` ### 移除 Comments ``` $sql = "-- This is a comment SELECT /* This is another comment On more than one line */ Id #This is one final comment as temp, DateCreated as Created FROM MyTable;"; echo SqlFormatter::removeComments($sql); /** SELECT Id as temp, DateCreated as Created FROM MyTable; */ ```