Format and highlight SQL statements in PHP

藏色散人
Release: 2023-04-07 11:52:02
forward
3080 people have browsed it

Format and highlight SQL statements in PHP

jdorn/sql-formatter is a lightweight PHP class used to format SQL statements.

It supports automatic indentation, adding newlines, and even supports syntax highlighting.

Used in the command line

This extension package contains a bin/sql-formatter executable file, which can be used directly for command line formatting SQL.

You can use this command after installing Composer globally:

composer global require jdorn/sql-formatter
sql-formatter "SELECT SOME QUERY;" // 直接格式化
// 或
echo "SELECT SOME QUERY;" | sql-formatter // 使用管道,更适合较大量的 SQL 语句
Copy after login

Use it as an extension package

The SqlFormatter class contains a static file named format. Method, which can receive a SQL statement string as a parameter and return formatted HTML code wrapped in pre tags.

For example:

= NOW()) )
    GROUP BY Column1 ORDER BY Column3 DESC LIMIT 5,10";
echo SqlFormatter::format($query);
Copy after login

Output:

Format and highlight SQL statements in PHP

##Only formatting without highlighting

If you do not need highlighting and only need to add indentation and line breaks, please set the second parameter to false.

Suitable for outputting error logs or other non-HTML data.

Copy after login

Output:

Format and highlight SQL statements in PHP

Only highlight without formatting

There is a separate method named highlight It can ensure that the original format is not changed, and only syntax highlighting is added.

Applies to when the SQL has been well formatted and needs to be made more readable.

Format and highlight SQL statements in PHP

Compress query statement

The compress method can remove all SQL comments and compress unnecessary spaces.

Suitable for outputting multiple query statements and making them easy to copy and paste into the command line.

-- 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($query);
Copy after login

Output:

SELECT Id as temp, DateCreated as Created FROM MyTable;
Copy after login

Remove comments

If you need to keep the original format, but still need to delete the SQL comments, you can use the removeComments method to replace compress.

-- 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($query);
Copy after login

Output:

    SELECT
    Id 
    as temp, DateCreated as Created FROM MyTable;
Copy after login

Split multiple SQL statements into arrays

There is also a feature that has nothing to do with formatting, which can split multiple SQL statements into arrays. SQL statements are separated into arrays.

For example:

DROP TABLE IF EXISTS MyTable;
CREATE TABLE MyTable ( id int );
INSERT INTO MyTable (id)
    VALUES
    (1),(2),(3),(4);
SELECT * FROM MyTable;
$queries = SqlFormatter::splitQuery($sql);
Copy after login

Result:

DROP TABLE IF EXISTS MyTable;
CREATE TABLE MyTable ( id int );
INSERT INTO MyTable (id) VALUES (1),(2),(3),(4);
SELECT * FROM MyTable;
Copy after login

Why not use regular expressions?

Go and read the README~

https://github.com/jdorn/sql-formatter#why....

The above is the detailed content of Format and highlight SQL statements in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!