Home Tools

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.
index.php
1<link href="user.css" rel="stylesheet" />
2<?php
3include_once("highlighter.class.php");
4$colorObj = new highlighter();
5$fileContent = $colorObj->applycolor("highlighter.class.php");
6echo $fileContent;
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","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
2include_once("keywords.php");
3class highlighter {
4	private $fileName;
5	private $fileExtension;
6	private $insideParenthesisColor;
7	private $insideBracketColor;
8	
9	public function __construct() {
10		$this->fileName = "";
11		$this->fileExtension = "";
12		//Color Configuration
13		$this->insideParenthesisColor = "iPC";
14		$this->insideBracketColor = "iBC";
15	}
16	
17	public function applycolor($fileLocation = "") {
18		if($fileLocation == "")
19		{	return;	}
20		else 
21		{
22			if(file_exists$fileLocation) {
23				$temp = explode("/",$fileLocation);
24				$this->fileName = trim(end$temp);
25				$temp = explode(".",$this->fileName);
26				$this->fileExtension = trim(end$temp);
27				$fileContent = trim(file_get_contents$fileLocation, true);
28				$fileContent = htmlentities($fileContent,ENT_NOQUOTES);
29				if($fileContent == "")
30				{	return;	}
31			}
32			else
33			{	return; }
34		}
35		
36		$blockFound = 0;
37		$blockFoundColor = array();
38		$parenthesisFound = 0;
39		$bracketFound = 0;
40		
41		$line = 1;
42		$characterBuffer = "";
43		$lastCharacter = "";
44		$counter = 0;
45		$contentSize = strlen($fileContent);
46
47		$outputContent ="<font class='lin'>".$line."</font> ";
48		while($counter < $contentSize) {
49			$character = $fileContent[$counter];
50			$code = intval(ord$character);
51			if($code >= 97 && $code <= 122 || $code >= 65 && $code <= 90)
52			{	$characterBuffer .= $character;	}
53			else
54			{
55				if($code == 9) {
56					$outputContent .= "    ";
57				}
58				else if($code == 10)	{	//Find EOL (End of Line)
59					if($characterBuffer != "") {
60						$outputContent .= $this->checker($characterBuffer);
61						$characterBuffer = "";
62					}
63					$line++;
64					if($blockFound == 0)
65					{	$outputContent .= $character."<font class='lin'>".$line."</font> ";	}
66					else
67					{	$outputContent .= "</font>".$character."<font class='lin'>".$line."</font> <font class='".$blockFoundColor[$blockFound-1]."'>";	}
68				}
69				else if($code == 32) {	//Find Space
70					if($characterBuffer != "") {	
71						$outputContent .= $this->checker($characterBuffer);
72						$characterBuffer = "";
73					}
74					$outputContent .= $character;
75				}
76				else if($character == "\"" || $character == "'") {	//Find Quotes
77					if($characterBuffer != "") {
78						$outputContent .= $this->checker($characterBuffer);	
79						$characterBuffer = "";
80					}
81					$outputContent .= "<font class='qC'>".$character;
82					$foundCharacter = $character;
83					$counter++;
84					while($counter < $contentSize) {
85						$character = $fileContent[$counter];
86						$code = intval(ord$character);
87						if($code == 9) {
88							$outputContent .= "    ";
89						}
90						else if($character == $foundCharacter) {
91							$outputContent .= $character;
92							if($lastCharacter == "\\") {
93								$lastCharacter = "";
94							}
95							else
96							{	break;	}
97						}
98						else if($character == "\\" && $lastCharacter == "\\") {
99							$outputContent .= $character;
100							$lastCharacter = "";
101						}
102						else
103						{
104							$lastCharacter = $character;
105							$code = intval(ord$character);
106							if($code != 10) 
107							{	$outputContent .= $character;	}
108							else
109							{
110								$line++;
111								$outputContent .= "</font>".$character."<font class='lin'>".$line."</font>	<font class='qC'>";
112							}
113						}
114						$counter++;
115					}
116					$outputContent .= "</font>";
117				}
118				else if($character == "(" || $character == ")") {	//Find Parenthesis
119					if($characterBuffer != "") {
120						$outputContent .= $this->checker($characterBuffer);	
121						$characterBuffer = "";
122					}
123					if($parenthesisFound == 0) {
124						$outputContent .= "<font class='pC'>".$character."</font><font class='iPC'>";
125						$parenthesisFound++;
126						$blockFoundColor[$blockFound] = $this->insideParenthesisColor;
127						$blockFound++;
128					}
129					else
130					{
131						if($character == "(") 
132						{	$parenthesisFound++;	}
133						if($character == ")") 
134						{	$parenthesisFound--;	}
135						if($parenthesisFound == 0) {
136							$outputContent .= "</font><font class='pC'>".$character."</font>";
137							$blockFound--;
138							unset($blockFoundColor[$blockFound]);
139						}
140						else
141						{	$outputContent .= $character;	}
142					}
143				}
144				else if($character == "[" || $character == "]")	{	//Find Bracket
145					if($characterBuffer != "") {
146						$outputContent .= $this->checker($characterBuffer);	
147						$characterBuffer = "";
148					}
149					if($bracketFound == 0) {
150						$outputContent .= "<font class='bC'>".$character."</font><font class='iBC'>";
151						$bracketFound++;
152						$blockFoundColor[$blockFound] = $this->insideBracketColor;
153						$blockFound++;
154					}
155					else
156					{
157						if($character == "[") 
158						{	$bracketFound++;	}
159						if($character == "]") 
160						{	$bracketFound--;	}
161						if($bracketFound == 0) {
162							$outputContent .= "</font><font class='bC'>".$character."</font>";
163							$blockFound--;
164							unset($blockFoundColor[$blockFound]);
165						}
166						else
167						{	$outputContent .= $character;	}
168					}
169				}
170				else if($character == "/" && isset$fileContent[$counter+1] && $fileContent[$counter+1] == "*" || $fileContent[$counter+1] == "/") {	//Find Comment
171					if($characterBuffer != "") {
172						$outputContent .= $this->checker($characterBuffer);	
173						$characterBuffer = "";
174					}
175					$outputContent .= "<font class='cC'>".$fileContent[$counter].$fileContent[$counter+1];
176					if($fileContent[$counter+1] == "*") {
177						$counter += 2;
178						while($counter < $contentSize) {
179							$character = $fileContent[$counter];
180							$code = intval(ord$character);
181							if($code == 9) {
182								$outputContent .= "    ";
183							}
184							else if($code != 10) {
185								if($character == "*" && isset$fileContent[$counter+1] && $fileContent[$counter+1] == "/") {
186									$counter++;
187									$outputContent .= $character.$fileContent[$counter]."</font>";
188									break;
189								}
190								else
191								{	$outputContent .= $character;	}
192							}
193							else
194							{
195								$line++;
196								$outputContent .= "</font>".$character."<font class='lin'>".$line."</font> <font class='cC'>";
197							}
198							$counter++;
199						}
200					}
201					else
202					{
203						$counter += 2;
204						while($counter < $contentSize) {
205							$character = $fileContent[$counter];
206							$code = intval(ord$character);
207							if($code == 10) {
208								$outputContent .= "</font>";
209								$counter--;
210								break;
211							}
212							$outputContent .= $character;
213							$counter++;
214						}
215					}
216				}
217				else if($characterBuffer != "") {
218					$outputContent .= $this->checker($characterBuffer).$character;
219					$characterBuffer = "";
220				}
221				else
222				{	$outputContent .= $character;	}
223			}
224			$counter++;
225		}
226		$rerurnData = "<div class='fN'>".$this->fileName."</div>";
227		$rerurnData .= "<pre><code><div class='codebox'>".$outputContent."</div></code></pre>";		
228		return $rerurnData;
229	}
230	
231	private function checker($value) {
232		global $languageKeywords;		
233		$value = trim($value);
234		if(isset$languageKeywords[$this->fileExtension]) {				
235			if(in_array$value,$languageKeywords[$this->fileExtension])
236			{	$value = "<font class='kC'>".$value."</font>";	}
237		}	
238		return $value;
239	}
240}
241?>
user.css
1/* code section start */		
2.codebox {
3padding: 19px;
4border-radius: 4px;
5-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .05);
6box-shadow: inset 0 1px 1px rgba(0, 0, 0, .05);
7line-height: 1.2000em;
8padding: 1.1%;
9font-size: 16px;
10overflow: auto;
11color:#59D9D1;
12background-color:#001628;
13border: 1px solid #e3e3e3;
14border-left: 4px solid #605a56;
15}
16
17.filenamestyle {
18font-size:17px;
19}
20
21.lin {
22color:#FFFFFF;
23}
24
25.fN {
26color:#286090;
27}
28
29.kC {
30color:#F5D67B;
31}
32
33.pC {
34color:#F5D67B;
35}
36
37.iPC {
38color:#F5D67B;
39}
40
41.bC {
42color:#F5D67B;
43}
44
45.iBC {
46color:#F5D67B;
47}
48
49.qC {
50color:#FC7038;
51}
52
53.cC {
54color:#b8b0b0;
55}
56/* code section end */