lib/SqlFormatter.php
public static function compress($string)
    {
        $result = '';

        $tokens = self::tokenize($string);

        $whitespace = true;
        foreach ($tokens as $token) {
            // Skip comment tokens
            if ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_COMMENT || $token[self::TOKEN_TYPE] === self::TOKEN_TYPE_BLOCK_COMMENT) {
                continue;
            }
            // Remove extra whitespace in reserved words (e.g "OUTER     JOIN" becomes "OUTER JOIN")
            elseif ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_RESERVED || $token[self::TOKEN_TYPE] === self::TOKEN_TYPE_RESERVED_NEWLINE || $token[self::TOKEN_TYPE] === self::TOKEN_TYPE_RESERVED_TOPLEVEL) {
                $token[self::TOKEN_VALUE] = preg_replace('/\s+/',' ',$token[self::TOKEN_VALUE]);
            }

            if ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_WHITESPACE) {
                // If the last token was whitespace, don't add another one
                if ($whitespace) {
                    continue;
                } else {
                    $whitespace = true;
                    // Convert all whitespace to a single space
                    $token[self::TOKEN_VALUE] = ' ';
                }
            } else {
                $whitespace = false;
            }

            $result .= $token[self::TOKEN_VALUE];
        }

        return rtrim($result);
    }
public static function compress($string)
    {
        $result = '';

        $tokens = self::tokenize($string);

        $whitespace = true;
        foreach ($tokens as $token) {
            // Skip comment tokens
            if ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_COMMENT || $token[self::TOKEN_TYPE] === self::TOKEN_TYPE_BLOCK_COMMENT) {
                continue;
            }
            // Remove extra whitespace in reserved words (e.g "OUTER     JOIN" becomes "OUTER JOIN")
            elseif ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_RESERVED || $token[self::TOKEN_TYPE] === self::TOKEN_TYPE_RESERVED_NEWLINE || $token[self::TOKEN_TYPE] === self::TOKEN_TYPE_RESERVED_TOPLEVEL) {
                $token[self::TOKEN_VALUE] = preg_replace('/\s+/',' ',$token[self::TOKEN_VALUE]);
            }

            if ($token[self::TOKEN_TYPE] === self::TOKEN_TYPE_WHITESPACE) {
                // If the last token was whitespace, don't add another one
                if ($whitespace) {
                    continue;
                } else {
                    $whitespace = true;
                    // Convert all whitespace to a single space
                    $token[self::TOKEN_VALUE] = ' ';
                }
            } else {
                $whitespace = false;
            }

            $result .= $token[self::TOKEN_VALUE];
        }

        return rtrim($result);
    }
public static function compress ( $ string ) { $ result = '' ; $ tokens = self :: tokenize ( $ string ) ; $ whitespace = true ; foreach ( $ tokens as $ token ) { // Skip comment tokens if ( $ token [ self :: TOKEN_TYPE ] === self :: TOKEN_TYPE_COMMENT || $ token [ self :: TOKEN_TYPE ] === self :: TOKEN_TYPE_BLOCK_COMMENT ) { continue ; } // Remove extra whitespace in reserved words (e.g "OUTER     JOIN" becomes "OUTER JOIN") elseif ( $ token [ self :: TOKEN_TYPE ] === self :: TOKEN_TYPE_RESERVED || $ token [ self :: TOKEN_TYPE ] === self :: TOKEN_TYPE_RESERVED_NEWLINE || $ token [ self :: TOKEN_TYPE ] === self :: TOKEN_TYPE_RESERVED_TOPLEVEL ) { $ token [ self :: TOKEN_VALUE ] = preg_replace ( '/\s+/' , ' ' , $ token [ self :: TOKEN_VALUE ] ) ; } if ( $ token [ self :: TOKEN_TYPE ] === self :: TOKEN_TYPE_WHITESPACE ) { // If the last token was whitespace, don't add another one if ( $ whitespace ) { continue ; } else { $ whitespace = true ; // Convert all whitespace to a single space $ token [ self :: TOKEN_VALUE ] = ' ' ; } } else { $ whitespace = false ; } $ result .= $ token [ self :: TOKEN_VALUE ] ; } return rtrim ( $ result ) ; }
Compress a query by collapsing white space and removing comments

@param String $string The SQL string

@return String The SQL string without comments
Compress a query by collapsing white space and removing comments