highlight-sql
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/*
Function: highlight_sql
Author: ME Wieringa <[email protected]>
Description: Highlight your query on the fly
*/
function highlight_sql($string)
{
$aKeywords = array();
// SQL syntax
$aKeywords[] = array('and', true); // keyword name (any string [a-zA-Z0-9_], or any character), keyword to next line (true or false, default: false), css class (default: 'keyword')
$aKeywords[] = array('as', false);
$aKeywords[] = array('asc', false);
$aKeywords[] = array('binary', false);
$aKeywords[] = array('by', false);
$aKeywords[] = array('delete', true);
$aKeywords[] = array('desc', false);
$aKeywords[] = array('from', true);
$aKeywords[] = array('having', true);
$aKeywords[] = array('group', true);
$aKeywords[] = array('insert', true);
$aKeywords[] = array('in', true);
$aKeywords[] = array('into', false);
$aKeywords[] = array('join', false);
$aKeywords[] = array('left', false);
$aKeywords[] = array('like', false);
$aKeywords[] = array('limit', true);
$aKeywords[] = array('order', true);
$aKeywords[] = array('on', false);
$aKeywords[] = array('or', true);
$aKeywords[] = array('right', false);
$aKeywords[] = array('select', true);
$aKeywords[] = array('set', true);
$aKeywords[] = array('values', true);
$aKeywords[] = array('where', true);
$aKeywords[] = array('xor', true);
// Operators
$aKeywords[] = array('+', false, 'operator');
$aKeywords[] = array('-', false, 'operator');
$aKeywords[] = array('*', false, 'operator');
$aKeywords[] = array('/', false, 'operator');
$aKeywords[] = array('=', false, 'operator');
$aKeywords[] = array('<', false, 'operator');
$aKeywords[] = array('>', false, 'operator');
$aKeywords[] = array('%', false, 'operator');
$aKeywords[] = array('.', false, 'operator');
$aKeywords[] = array(',', false, 'operator');
$aKeywords[] = array('true', false, 'quoted');
$aKeywords[] = array('false', false, 'quoted');
$aKeywords[] = array('null', false, 'quoted');
$aKeywords[] = array('unkown', false, 'quoted');
// Split query into pieces (quoted values, ticked values, string and/or numeric values, and all others).
$expr = '/(\'((\\\\.)|[^\\\\\\\'])*\')|(\`((\\\\.)|[^\\\\\\\`])*\`)|([a-z0-9_]+)|([\s\n]+)|(.)/i';
preg_match_all($expr, $string, $matches);
// Use a buffer to build up lines.
$buffer = '';
// Keep track of brackets to indent/outdent
$iTab = 0;
for($i = 0; $i < sizeof($matches[0]); $i++)
{
if(strcasecmp($match = $matches[0][$i], "") !== 0)
{
if(in_array($match, array("(", ")"))) // Bracket found
{
$buffer = trim($buffer);
if(strlen($buffer) > 0)
{
$result .= $buffer . '<br>';
}
$buffer = '';
if(strcasecmp($match, ")") === 0)
{
$iTab--;
if($iTab < 0)
{
$iTab = 0;
}
$result .= str_repeat(' ', 4 * $iTab) . '<span class="bracket">' . htmlentities($match) . '</span><br>';
}
else // if(strcasecmp($match, "(") === 0)
{
$result .= str_repeat(' ', 4 * $iTab) . '<span class="bracket">' . htmlentities($match) . '</span><br>';
$iTab++;
}
}
elseif(preg_match('/^[\s\n]+$/', $match)) // Space character(s)
{
if(strlen($buffer) === 0)
{
// Ignore space character(s)!
}
else
{
$buffer .= ' ';
}
}
else
{
$aKeyword = false;
for($j = 0; $j < sizeof($aKeywords); $j++)
{
if(strcasecmp($match, $aKeywords[$j][0]) === 0)
{
$aKeyword = $aKeywords[$j];
break;
}
}
if($aKeyword) // Keyword found
{
if(isset($aKeyword[1]) && $aKeyword[1] === true) // Keyword to next line
{
$buffer = trim($buffer);
if(strlen($buffer) > 0)
{
$result .= $buffer . '<br>';
}
$buffer = '';
}
if(strlen($buffer) === 0) // Indent
{
$buffer .= str_repeat(' ', 4 * $iTab);
}
$buffer .= '<span class="' . (isset($aKeyword[2]) ? $aKeyword[2] : 'keyword') . '">' . htmlentities(strtoupper($match)) . '</span>';
}
else
{
if(strlen($buffer) === 0) // Indent
{
$buffer = str_repeat(' ', 4 * $iTab);
}
if((strcasecmp(substr($match, 0, 1), "'") === 0) || is_numeric($match)) // Quoted value or number
{
$buffer .= '<span class="quoted">' . htmlentities($match) . '</span>';
}
elseif((strcasecmp(substr($match, 0, 1), "`") === 0) || preg_match('/[a-z0-9_]+/i', $match)) // Ticked value or unquoted string (table/column name?!)
{
$buffer .= '<span class="ticked">' . htmlentities($match) . '</span>';
}
else // All other chars
{
$buffer .= htmlentities($match);
}
}
}
}
}
$buffer = trim($buffer);
if(strlen($buffer) > 0)
{
$result .= $buffer;
}
return '<code class="sql">' . $result . '</code>';
}
?>
/*
Function: highlight_sql
Author: ME Wieringa <[email protected]>
Description: Highlight your query on the fly
*/
function highlight_sql($string)
{
$aKeywords = array();
// SQL syntax
$aKeywords[] = array('and', true); // keyword name (any string [a-zA-Z0-9_], or any character), keyword to next line (true or false, default: false), css class (default: 'keyword')
$aKeywords[] = array('as', false);
$aKeywords[] = array('asc', false);
$aKeywords[] = array('binary', false);
$aKeywords[] = array('by', false);
$aKeywords[] = array('delete', true);
$aKeywords[] = array('desc', false);
$aKeywords[] = array('from', true);
$aKeywords[] = array('having', true);
$aKeywords[] = array('group', true);
$aKeywords[] = array('insert', true);
$aKeywords[] = array('in', true);
$aKeywords[] = array('into', false);
$aKeywords[] = array('join', false);
$aKeywords[] = array('left', false);
$aKeywords[] = array('like', false);
$aKeywords[] = array('limit', true);
$aKeywords[] = array('order', true);
$aKeywords[] = array('on', false);
$aKeywords[] = array('or', true);
$aKeywords[] = array('right', false);
$aKeywords[] = array('select', true);
$aKeywords[] = array('set', true);
$aKeywords[] = array('values', true);
$aKeywords[] = array('where', true);
$aKeywords[] = array('xor', true);
// Operators
$aKeywords[] = array('+', false, 'operator');
$aKeywords[] = array('-', false, 'operator');
$aKeywords[] = array('*', false, 'operator');
$aKeywords[] = array('/', false, 'operator');
$aKeywords[] = array('=', false, 'operator');
$aKeywords[] = array('<', false, 'operator');
$aKeywords[] = array('>', false, 'operator');
$aKeywords[] = array('%', false, 'operator');
$aKeywords[] = array('.', false, 'operator');
$aKeywords[] = array(',', false, 'operator');
$aKeywords[] = array('true', false, 'quoted');
$aKeywords[] = array('false', false, 'quoted');
$aKeywords[] = array('null', false, 'quoted');
$aKeywords[] = array('unkown', false, 'quoted');
// Split query into pieces (quoted values, ticked values, string and/or numeric values, and all others).
$expr = '/(\'((\\\\.)|[^\\\\\\\'])*\')|(\`((\\\\.)|[^\\\\\\\`])*\`)|([a-z0-9_]+)|([\s\n]+)|(.)/i';
preg_match_all($expr, $string, $matches);
// Use a buffer to build up lines.
$buffer = '';
// Keep track of brackets to indent/outdent
$iTab = 0;
for($i = 0; $i < sizeof($matches[0]); $i++)
{
if(strcasecmp($match = $matches[0][$i], "") !== 0)
{
if(in_array($match, array("(", ")"))) // Bracket found
{
$buffer = trim($buffer);
if(strlen($buffer) > 0)
{
$result .= $buffer . '<br>';
}
$buffer = '';
if(strcasecmp($match, ")") === 0)
{
$iTab--;
if($iTab < 0)
{
$iTab = 0;
}
$result .= str_repeat(' ', 4 * $iTab) . '<span class="bracket">' . htmlentities($match) . '</span><br>';
}
else // if(strcasecmp($match, "(") === 0)
{
$result .= str_repeat(' ', 4 * $iTab) . '<span class="bracket">' . htmlentities($match) . '</span><br>';
$iTab++;
}
}
elseif(preg_match('/^[\s\n]+$/', $match)) // Space character(s)
{
if(strlen($buffer) === 0)
{
// Ignore space character(s)!
}
else
{
$buffer .= ' ';
}
}
else
{
$aKeyword = false;
for($j = 0; $j < sizeof($aKeywords); $j++)
{
if(strcasecmp($match, $aKeywords[$j][0]) === 0)
{
$aKeyword = $aKeywords[$j];
break;
}
}
if($aKeyword) // Keyword found
{
if(isset($aKeyword[1]) && $aKeyword[1] === true) // Keyword to next line
{
$buffer = trim($buffer);
if(strlen($buffer) > 0)
{
$result .= $buffer . '<br>';
}
$buffer = '';
}
if(strlen($buffer) === 0) // Indent
{
$buffer .= str_repeat(' ', 4 * $iTab);
}
$buffer .= '<span class="' . (isset($aKeyword[2]) ? $aKeyword[2] : 'keyword') . '">' . htmlentities(strtoupper($match)) . '</span>';
}
else
{
if(strlen($buffer) === 0) // Indent
{
$buffer = str_repeat(' ', 4 * $iTab);
}
if((strcasecmp(substr($match, 0, 1), "'") === 0) || is_numeric($match)) // Quoted value or number
{
$buffer .= '<span class="quoted">' . htmlentities($match) . '</span>';
}
elseif((strcasecmp(substr($match, 0, 1), "`") === 0) || preg_match('/[a-z0-9_]+/i', $match)) // Ticked value or unquoted string (table/column name?!)
{
$buffer .= '<span class="ticked">' . htmlentities($match) . '</span>';
}
else // All other chars
{
$buffer .= htmlentities($match);
}
}
}
}
}
$buffer = trim($buffer);
if(strlen($buffer) > 0)
{
$result .= $buffer;
}
return '<code class="sql">' . $result . '</code>';
}
?>
De beschikbare CSS tags zijn (naar eigen inzicht gestijlt):
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<style type="text/css">
code.sql
{
background: #FCFCFC;
border: #E8E8E8 solid 1px;
color: #000000;
display: block;
padding: 20px;
}
code.sql .keyword
{
color: #990099;
}
code.sql .operator
{
color: #FF00FF;
}
code.sql .quoted
{
color: #008000;
}
code.sql .ticked
{
color: #000080;
}
code.sql .bracket
{
color: #FF00FF;
}
</style>
code.sql
{
background: #FCFCFC;
border: #E8E8E8 solid 1px;
color: #000000;
display: block;
padding: 20px;
}
code.sql .keyword
{
color: #990099;
}
code.sql .operator
{
color: #FF00FF;
}
code.sql .quoted
{
color: #008000;
}
code.sql .ticked
{
color: #000080;
}
code.sql .bracket
{
color: #FF00FF;
}
</style>
Edit: Gewijzigd n.a.v. feedback cervetti, tnx!
Edit: Gewijzigd n.a.v. feedback Elwin, tnx!
Edit: 2e release, herziene versie m.b.v. regex.
Edit: Gewijzigd n.a.v. feedback Tumbler, tnx!
Edit: Gewijzigd n.a.v. feedback Jim, tnx!
Tevens (meerdere) tabs/spaties/enters zijn nu afgevangen.