How to develop a source code syntax highlighter using PHP

Syntax highlighter is used to show the source code program is colorful, so the reader can easily read/understand your code after integration. In this program, I added various elements (reserve words, parenthesis, comment, and quotes etc..) for highlighting. You can add/modify this code based on your Web Application/Programming Blog.

[Download Source code]

This syntax highlighter code is independent to any language, so you add integration with any programming language.

Note: I use this script in this blog also, please customize the code based on your requirement.

index.php
1 <link href="highlighter.css" rel="stylesheet">
2 <?php
3 include_once("highlighter.class.php");
4 $obj = new highlighter();
5 $fileLocation = "/home/thanshitha/workspace/storyofalgorithm/src/in/blogspot/storyofalgorithm/LinkedList.java";
6 echo $obj->applycolor($fileLocation);
7 ?>
Please refer below config file for introducing new programming file type extension or add more keywords.
keywords.php
1 <?php
2 //Programming language keywords
3 $languageKeywords["php"] = array("if","else","isset","unset","array","foreach","for","while","do","new","private","public","protected");
4 $languageKeywords["cpp"] = array("void","char","int","main","static","if","else","for","while","do","cout","cin","struct","unsigned","long","return");
5 $languageKeywords["java"] = array("if","else","for","while","do","return","public","private","static","void","import","class","try","catch","package","main","this","int","null","new");
6 ?>
highlighter.class.php
1 <?php
2 include_once("keywords.php");
3 class highlighter {
4
5 private $fileName;
6 private $fileNameColor;
7 private $fileExtension;
8 private $parenthesisColor;
9 private $insideParenthesisColor;
10 private $keywordColor;
11 private $backGroundColor;
12 private $borderColor;
13 private $leftBorderColor;
14 private $quotesColor;
15 private $commentColor;
16
17 public function __construct() {
18 $this->fileName = "";
19
20 $this->fileExtension = "";
21
22 //Color Configuration
23 $this->fileNameColor = "#286090";
24 $this->keywordColor = "green";
25 $this->backGroundColor = "#fdfefe";
26
27 $this->borderColor = "#e3e3e3";
28 $this->leftBorderColor = "#605a56";
29
30 $this->parenthesisColor = "#ec7700";
31 $this->insideParenthesisColor = "#ec7700";
32 $this->bracketColor = "#ec7700";
33 $this->insideBracketColor = "#ec7700";
34
35 $this->quotesColor = "#6a2c70";
36 $this->commentColor = "#b8b0b0";
37 }
38
39 public function applycolor($fileLocation = "") {
40 if($fileLocation == "")
41 { return; }
42 else
43 {
44 if(file_exists($fileLocation)) {
45 $temp = explode("/",$fileLocation);
46 $this->fileName = trim(end($temp));
47 $temp = explode(".",$this->fileName);
48 $this->fileExtension = trim(end($temp));
49 $fileContent = trim(file_get_contents($fileLocation, true));
50 $fileContent = htmlentities($fileContent,ENT_NOQUOTES);
51 if($fileContent == "")
52 { return; }
53 }
54 else
55 { return; }
56 }
57
58 $line = 1;
59 $outputContent = "<div class=\"divblock\"><b>".$line."</b> ";
60 $characterBuffer = "";
61
62 $blockFound = 0;
63 $blockFoundColor = array();
64 $parenthesisFound = 0;
65 $bracketFound = 0;
66
67 $counter = 0;
68 $lastCharacter = "";
69 $contentSize = strlen($fileContent);
70
71 while($counter < $contentSize) {
72 $character = $fileContent[$counter];
73 $code = intval(ord($character));
74 if($blockFound == 0 && (($code >= 97 && $code <= 122) || ($code >= 65 && $code <= 90))) //Fnd alphabetic characters
75 { $characterBuffer .= $character; }
76 else
77 {
78 if($code == 10) { //Find EOL (End of Line)
79 if($this->checker($characterBuffer))
80 { $characterBuffer = "<font color='".$this->keywordColor."'>".$characterBuffer."</font>"; }
81 $line++;
82 if($blockFound == 0)
83 { $outputContent .= $characterBuffer."</div>".$character."<div class=\"divblock\"><b>".$line."</b> "; }
84 else
85 { $outputContent .= $characterBuffer."</font></div>".$character."<div class=\"divblock\"><b>".$line."</b> <font color='".$blockFoundColor[$blockFound-1]."'>"; }
86 $characterBuffer = "";
87 }
88 else if($code == 32) { //Find Space
89 if($characterBuffer != "") {
90 if($this->checker($characterBuffer))
91 { $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>".$character; }
92 else
93 { $outputContent .= $characterBuffer.$character; }
94 $characterBuffer = "";
95 }
96 else
97 { $outputContent .= $character; }
98 }
99 else if($character == "\"" || $character == "'") { //Find Quotes
100 if($characterBuffer != "")
101 {
102 if($this->checker($characterBuffer))
103 { $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>"; }
104 else
105 { $outputContent .= $characterBuffer; }
106 $characterBuffer = "";
107 }
108 $outputContent .= "<font color='".$this->quotesColor."'>".$character;
109 $foundCharacter = $character;
110 $counter++;
111 while($counter < $contentSize) {
112 $character = $fileContent[$counter];
113 if($character == $foundCharacter) {
114 $outputContent .= $character;
115 if($lastCharacter == "\\") {
116 $lastCharacter = "";
117 }
118 else
119 { break; }
120 }
121 else if($character == "\\" && $lastCharacter == "\\") {
122 $outputContent .= $character;
123 $lastCharacter = "";
124 }
125 else
126 {
127 $lastCharacter = $character;
128 $code = intval(ord($character));
129 if($code != 10)
130 { $outputContent .= $character; }
131 else
132 {
133 $line++;
134 $outputContent .= "</font></div>".$character."<div class=\"divblock\"><b>".$line."</b> <font color='".$this->quotesColor."'>";
135 }
136 }
137 $counter++;
138 }
139 $outputContent .= "</font>";
140 }
141 else if($character == "(" || $character == ")") { //Find Parenthesis
142 if($characterBuffer != "")
143 {
144 if($this->checker($characterBuffer))
145 { $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>"; }
146 else
147 { $outputContent .= $characterBuffer; }
148 $characterBuffer = "";
149 }
150 if($parenthesisFound == 0) {
151 $blockFoundColor[$blockFound] = $this->insideParenthesisColor;
152 $outputContent .= "<font color='".$this->parenthesisColor."'>".$character."</font><font color='".$this->insideParenthesisColor."'>";
153 $parenthesisFound++;
154 $blockFound++;
155 }
156 else
157 {
158 if($character == "(") {
159 $parenthesisFound++;
160 }
161 if($character == ")") {
162 $parenthesisFound--;
163 }
164 if($parenthesisFound == 0) {
165 $outputContent .= "</font><font color='".$this->parenthesisColor."'>".$character."</font>";
166 $blockFound--;
167 unset($blockFoundColor[$blockFound]);
168 }
169 else
170 { $outputContent .= $character; }
171 }
172 }
173 else if($character == "[" || $character == "]") { //Find Bracket
174 if($characterBuffer != "")
175 {
176 if($this->checker($characterBuffer))
177 { $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>"; }
178 else
179 { $outputContent .= $characterBuffer; }
180 $characterBuffer = "";
181 }
182 if($bracketFound == 0) {
183 $blockFoundColor[$blockFound] = $this->insideBracketColor;
184 $outputContent .= "<font color='".$this->bracketColor."'>".$character."</font><font color='".$this->insideBracketColor."'>";
185 $bracketFound++;
186 $blockFound++;
187 }
188 else
189 {
190 if($character == "[") {
191 $bracketFound++;
192 }
193 if($character == "]") {
194 $bracketFound--;
195 }
196 if($bracketFound == 0) {
197 $outputContent .= "</font><font color='".$this->bracketColor."'>".$character."</font>";
198 $blockFound--;
199 unset($blockFoundColor[$blockFound]);
200 }
201 else
202 { $outputContent .= $character; }
203 }
204 }
205 else if($character == "/" && (isset($fileContent[$counter+1]) && ($fileContent[$counter+1] == "*" || $fileContent[$counter+1] == "/"))) { //Find Comment
206 if($characterBuffer != "")
207 {
208 if($this->checker($characterBuffer))
209 { $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>"; }
210 else
211 { $outputContent .= $characterBuffer; }
212 $characterBuffer = "";
213 }
214 $blockFound++;
215 $outputContent .= "<font color='".$this->commentColor."'>".$fileContent[$counter].$fileContent[$counter+1];
216 if($fileContent[$counter+1] == "*") {
217 $counter += 2;
218 $checkCharacter = "*";
219 while($counter < $contentSize) {
220 $outputContent .= $fileContent[$counter];
221 if($fileContent[$counter] == $checkCharacter) {
222 if($checkCharacter == "*")
223 { $checkCharacter = "/"; }
224 else
225 {
226 $blockFound--;
227 $outputContent .= "</font>";
228 break;
229 }
230 }
231 $counter++;
232 }
233 }
234 else
235 {
236 $counter += 2;
237 while($counter < $contentSize) {
238 $character = $fileContent[$counter];
239 $code = intval(ord($character));
240 if($code == 10) {
241 $counter--;
242 $blockFound--;
243 $outputContent .= "</font>";
244 break;
245 }
246 $outputContent .= $character;
247 $counter++;
248 }
249 }
250 }
251 else if($characterBuffer != "")
252 {
253 if($this->checker($characterBuffer))
254 { $outputContent .= "<font color='".$this->keywordColor."'>".$characterBuffer."</font>".$character; }
255 else
256 { $outputContent .= $characterBuffer.$character; }
257 $characterBuffer = "";
258 }
259 else
260 { $outputContent .= $character; }
261 }
262 $counter++;
263 }
264 $outputContent .= "</div>";
265
266 $rerurnData = "<div class='filenamestyle' style='color:".$this->fileNameColor.";'>".$this->fileName."</div>"; //Show filename
267 $rerurnData .= "<div><pre><div class='codebox' style='background-color:".$this->backGroundColor.";border: 1px solid ".$this->borderColor.";border-left: 4px solid ".$this->leftBorderColor.";'>".$outputContent."</div></pre></div>";
268 return $rerurnData;
269 }
270
271 private function checker($value) {
272 global $languageKeywords;
273 if(isset($languageKeywords[$this->fileExtension])) {
274 $value = trim($value);
275 if(in_array($value,$languageKeywords[$this->fileExtension]))
276 { return 1; }
277 else
278 { return 0; }
279 }
280 }
281 }
282 ?>
highlighter.css
1 .codebox
2 {
3 max-height: 800px;
4 padding: 19px;
5 margin-bottom: 20px;
6 border-radius: 4px;
7 -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .05);
8 box-shadow: inset 0 1px 1px rgba(0, 0, 0, .05);
9 line-height: 1.2000em;
10 padding: 1.1%;
11 font-family: Consolas,"courier new";
12 font-size: 15px;
13 overflow: auto;
14 }
15
16 .filenamestyle
17 {
18 font-size:17px;
19 }
20
21 .divblock {
22 display:inline-block;
23 }

0 Comments

Leave a Comment:

(Your email address will not be published. Required fields are marked *)

Name *

Email *

Message *

Social Media