How to develop a source code syntax highlighter using php
Syntax highlighter is uses to show the source code program in colorful, so the reader can easily read your code. I added various elements (keywords, parenthesis, comment, and quotes etc..) for highlighting. You can add or modify this code based on your programming blog design. For our testing purpose I created below sample test file.index.php
1 <link href="style.css" rel="stylesheet" />
2 <?php
3 include_once("highlighter.class.php");
4 $colorObj = new highlighter();
5 $fileContent = $colorObj->applycolor("highlighter.class.php");
6 echo $fileContent;
7 $colorObj->showfilename(false);
8 $fileContent = $colorObj->applycolor("index.php");
9 echo $fileContent;
10 ?>
highlighter.class.php
1 <?php
2 include_once("keywords.php");
3 class Highlighter {
4 private $fileName;
5 private $fileExtension;
6 private $showFileName;
7
8 public function __construct() {
9 $this->fileName = "";
10 $this->fileExtension = "";
11 $this->showFileName = true;
12 }
13
14 public function showfilename($value) {
15 $this->showFileName = $value;
16 }
17
18 public function applycolor($fileLocation = "") {
19 if($fileLocation == "") {
20 return;
21 }
22 else
23 {
24 if(file_exists$fileLocation) {
25 $temp = explode("/",$fileLocation);
26 $this->fileName = trim(end$temp);
27 $temp = explode(".",$this->fileName);
28 $this->fileExtension = trim(end$temp);
29 $fileContent = trim(file_get_contents$fileLocation, true);
30 $fileContent = htmlentities($fileContent,ENT_NOQUOTES);
31 if($fileContent == "") {
32 return;
33 }
34 }
35 else
36 {
37 return;
38 }
39
40 $parenthesisFound = 0;
41 $bracketFound = 0;
42 $foundCharacter = "";
43
44 $line = 1;
45 $counter = 0;
46 $contentSize = strlen($fileContent);
47
48 $content = "<font class='lI'>".$line."</font> ";
49 while($counter < $contentSize) {
50 $character = $fileContent[$counter];
51 $code = intval(ord$character);
52 if($code >= 97 && $code <= 122 || $code >= 65 && $code <= 90) { //Identify only alphabet(Capital/Small) characters
53 $characterBuffer .= $character;
54 }
55 else
56 {
57 if($characterBuffer != "") {
58 $content .= $this->checker($characterBuffer);
59 $characterBuffer = "";
60 }
61
62 if($character == "/" && isset$fileContent[$counter+1] && $fileContent[$counter+1] == "*" || $fileContent[$counter+1] == "/") { //Identify single comment or multiple comments
63 $content .= "<font class='cC'>".$fileContent[$counter].$fileContent[$counter+1];
64 if($fileContent[$counter+1] == "*") {
65 $counter += 2;
66 while($counter < $contentSize) {
67 $character = $fileContent[$counter];
68 $code = intval(ord$character);
69 if($code != 10) { //Identify '\n' character
70 if($character == "*" && isset$fileContent[$counter+1] && $fileContent[$counter+1] == "/") {
71 $counter++;
72 $content .= $character.$fileContent[$counter]."</font>";
73 break;
74 }
75 else
76 { $content .= $character; }
77 }
78 else
79 {
80 $line++;
81 $content .= "</font>".$character."<font class='lI'>".$line."</font> <font class='cC'>";
82 }
83 $counter++;
84 }
85 }
86 else
87 {
88 $counter += 2;
89 while($counter < $contentSize) {
90 $character = $fileContent[$counter];
91 $code = intval(ord$character);
92 if($code == 10) { //Identify '\n' character
93 $content .= "</font>";
94 $counter--;
95 break;
96 }
97 $content .= $character;
98 $counter++;
99 }
100 }
101 }
102 else if($character == "'" || $character == "\"") { //Identify sigle quote or double quote character
103 $foundCharacter = $character;
104 $content .= "<font class='qC'>".$foundCharacter;
105 $counter++;
106 while($counter < $contentSize) {
107 $character = $fileContent[$counter];
108 $code = intval(ord$character);
109 if($foundCharacter == $character) {
110 if($foundCharacter == "\"") {
111 if($fileContent[$counter-1] != "\\") {
112 $content .= $foundCharacter."</font>";
113 break;
114 }
115 else if($fileContent[$counter-2] == "\\" && $fileContent[$counter-1] == "\\") {
116 $content .= $foundCharacter."</font>";
117 break;
118 }
119 else
120 {
121 $content .= $character;
122 }
123 }
124 else
125 {
126 $content .= $foundCharacter."</font>";
127 break;
128 }
129 }
130 else if($code == 10) { //Identify '\n' character
131 $line++;
132 $content .= $character;
133 $content .= "<font class='lI'>".$line."</font> ";
134 }
135 else
136 {
137 $content .= $character;
138 }
139 $counter++;
140 }
141 }
142 else if($character == "(" || $character == ")") { //Identify parenthesis character
143 if($parenthesisFound == 0) {
144 $content .= "<font class='pC'>".$character."</font><font class='iPC'>";
145 }
146 if($character == "(") {
147 $parenthesisFound++;
148 }
149 else if($character == ")") {
150 $parenthesisFound--;
151 }
152 if($parenthesisFound == 0) {
153 $content .= "</font><font class='pC'>".$character."</font>";
154 }
155 }
156 else if($character == "[" || $character == "]") { //Identify bracket character
157 if($bracketFound == 0) {
158 $content .= "<font class='bC'>".$character."</font><font class='iBC'>";
159 }
160 if($character == "[") {
161 $bracketFound++;
162 }
163 else if($character == "]") {
164 $bracketFound--;
165 }
166 if($bracketFound == 0) {
167 $content .= "</font><font class='bC'>".$character."</font>";
168 }
169 }
170 else if($code == 10) { //Identify '\n' character
171 $line++;
172 $content .= $character;
173 $content .= "<font class='lI'>".$line."</font> ";
174 }
175 else
176 {
177 $content .= $character;
178 }
179 }
180 $counter++;
181 }
182
183 $output .= "<div class='codebox'>";
184 if($this->showFileName == true) {
185 $output .= "<div class='fN'>".$this->fileName."</div>";
186 }
187 $output .= "<div class='code'><pre><code>".$content."</code></pre></div>";
188 $output .= "</div>";
189 return $output;
190 }
191 }
192
193 private function checker($value) {
194 global $languageKeywords;
195 $value = trim($value);
196 if(isset$languageKeywords[$this->fileExtension]) { //Identify file type extension
197 if(in_array$value,$languageKeywords[$this->fileExtension]) //Identify keywords
198 { $value = "<font class='kC'>".$value."</font>"; }
199 }
200 return $value;
201 }
202 }
203 ?>
keywords.php
1 <?php
2 //Programming language keywords
3 $languageKeywords["php"] = array("function","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 ?>
style.css
1 .codebox {
2 margin-top:8px;
3 margin-bottom:8px;
4 font-size:16px;
5 }
6
7 .fN {
8 color:#21094e;
9 margin-bottom:5px;
10 }
11
12 .code {
13 padding:16px;
14 overflow:auto;
15 color:#59D9D1;
16 background-color:#000000;
17 border-left:5px solid #605a56;
18 }
19
20 pre {
21 margin:0px;
22 padding:0px;
23 }
24
25 .lI {
26 color:#ffffff;
27 }
28
29 .kC {
30 color:#F5D67B;
31 }
32
33 .cC {
34 color:#b8b0b0;
35 }
36
37 .qC {
38 color:#FC7038;
39 }
40
41 .pC {
42 color:#F5D67B;
43 }
44
45 .iPC {
46 color:#F5D67B;
47 }
48
49 .bC {
50 color:#F5D67B;
51 }
52
53 .iBC {
54 color:#F5D67B;
55 }