How to create a PHP Minifier
PHP Minifier is the process of removing all unnecessary characters from source code without changing their functionality. Unnecessary characters usually include tab (/t) space characters, new line characters, single (//,#) and multiple lines (/* .. */) comments.It reduces the size of the source code, so its transmission the output over the internet as more efficient. For our testing purpose I created below sample test file.index.php
1 <?php
2 include_once("PHPMinifier.php");
3 $obj = new PHPMinifier();
4 $obj->apply("PHPMinifier.php");
5 ?>
PHPMinifier.php
1 <?php
2 class PHPMinifier {
3
4 private $fileName;
5
6 public function __construct() {
7 $this->fileName = "";
8 }
9
10 public function apply($fileLocation = "") {
11 if($fileLocation == "")
12 { return; }
13 else
14 {
15 if(file_exists$fileLocation) {
16 $temp = explode("/",$fileLocation);
17 $this->fileName = trim(end$temp);
18 $fileContent = trim(file_get_contents$fileLocation, true);
19 if($fileContent == "")
20 { return; }
21 }
22 else
23 { return; }
24
25 $minifierString = "";
26 $lastCharacter = "";
27
28 $counter = 0;
29 $contentSize = strlen($fileContent);
30 while($counter < $contentSize) {
31 $character = $fileContent[$counter];
32 $code = intval(ord$character);
33 if($code == 10) //Find EOL (End of Line)
34 { $minifierString .= " "; }
35 else if($character == "\t") //Find Tab
36 { $minifierString .= ""; }
37 else if($character == "#") { //Find Single Line (#.....) Comment
38 $counter++;
39 while($counter < $contentSize) {
40 $character = $fileContent[$counter];
41 $code = intval(ord$character);
42 if($code == 10) { //Find EOL (End of Line)
43 $minifierString .= " ";
44 break;
45 }
46 $counter++;
47 }
48 }
49 else if($character == "\"" || $character == "'") { //Find Double or Single Quote
50 $minifierString .= $character;
51 $foundCharacter = $character;
52 $counter++;
53 while($counter < $contentSize) {
54 $character = $fileContent[$counter];
55 if($character == $foundCharacter) {
56 $minifierString .= $character;
57 if($lastCharacter == "\\") {
58 $lastCharacter = "";
59 }
60 else
61 { break; }
62 }
63 else if($character == "\\" && $lastCharacter == "\\") {
64 $minifierString .= $character;
65 $lastCharacter = "";
66 }
67 else
68 {
69 $lastCharacter = $character;
70 $code = intval(ord$character);
71 if($code != 10)
72 { $minifierString .= $character; }
73 else
74 { $minifierString .= " "; }
75 }
76 $counter++;
77 }
78 }
79 else if($character == "/" && isset$fileContent[$counter+1] && $fileContent[$counter+1] == "*" || $fileContent[$counter+1] == "/") { // Single (//....) and Multiple Lines (/* */) Comment
80 if($fileContent[$counter+1] == "*") {
81 $counter += 2;
82 $checkCharacter = "*";
83 while($counter < $contentSize) {
84 if($fileContent[$counter] == $checkCharacter) {
85 if($checkCharacter == "*")
86 { $checkCharacter = "/"; }
87 else
88 {
89 $minifierString .= " ";
90 break;
91 }
92 }
93 $counter++;
94 }
95 }
96 else
97 {
98 $counter += 2;
99 while($counter < $contentSize) {
100 $character = $fileContent[$counter];
101 $code = intval(ord$character);
102 if($code == 10) {
103 $minifierString .= " ";
104 break;
105 }
106 $counter++;
107 }
108 }
109 }
110 else
111 { $minifierString .= $character; }
112 $counter++;
113 }
114 echo $minifierString;
115
116 //Please remove the below comment, if you want to store the compress minifier string in file (compress-minifier.txt), please check this file permission (read/write) also.
117 /*
118 $outputFile = "./compress-minifier.txt";
119 $fileObj = fopen($outputFile, "w+") or die("Unable to create/open file!");
120 fwrite($fileObj, $minifierString);
121 fclose($fileObj);
122 if(file_exists($outputFile))
123 { echo "Please find the minifier script in <b>[".$outputFile."]</b>"; }
124 else
125 { echo "Couldn't able to produce the output"; }
126 */
127 }
128 }
129 }
130 ?>