How to develop 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.

[Download Source code]

index.php
1 <?php
2 include_once("PHPMinifier.php");
3 $obj = new PHPMinifier();
4 $obj->apply("page.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
12 if($fileLocation == "")
13 { return; }
14 else
15 {
16 if(file_exists($fileLocation)) {
17 $temp = explode("/",$fileLocation);
18 $this->fileName = trim(end($temp));
19 $fileContent = trim(file_get_contents($fileLocation, true));
20 if($fileContent == "")
21 { return; }
22 }
23 else
24 { return; }
25
26 $minifierString = "";
27 $lastCharacter = "";
28
29 $counter = 0;
30 $contentSize = strlen($fileContent);
31 while($counter < $contentSize) {
32 $character = $fileContent[$counter];
33 $code = intval(ord($character));
34 if($code == 10) //Find EOL (End of Line)
35 { $minifierString .= " "; }
36 else if($character == "\t") //Find Tab
37 { $minifierString .= ""; }
38 else if($character == "#") { //Find Single Line (#.....) Comment
39 $counter++;
40 while($counter < $contentSize) {
41 $character = $fileContent[$counter];
42 $code = intval(ord($character));
43 if($code == 10) { //Find EOL (End of Line)
44 $minifierString .= " ";
45 break;
46 }
47 $counter++;
48 }
49 }
50 else if($character == "\"" || $character == "'") { //Find Double or Single Quote
51 $minifierString .= $character;
52 $foundCharacter = $character;
53 $counter++;
54 while($counter < $contentSize) {
55 $character = $fileContent[$counter];
56 if($character == $foundCharacter) {
57 $minifierString .= $character;
58 if($lastCharacter == "\\") {
59 $lastCharacter = "";
60 }
61 else
62 { break; }
63 }
64 else if($character == "\\" && $lastCharacter == "\\") {
65 $minifierString .= $character;
66 $lastCharacter = "";
67 }
68 else
69 {
70 $lastCharacter = $character;
71 $code = intval(ord($character));
72 if($code != 10)
73 { $minifierString .= $character; }
74 else
75 { $minifierString .= " "; }
76 }
77 $counter++;
78 }
79 }
80 else if($character == "/" && (isset($fileContent[$counter+1]) && ($fileContent[$counter+1] == "*" || $fileContent[$counter+1] == "/"))) { // Single (//....) and Multiple Lines (/* */) Comment
81 if($fileContent[$counter+1] == "*") {
82 $counter += 2;
83 $checkCharacter = "*";
84 while($counter < $contentSize) {
85 if($fileContent[$counter] == $checkCharacter) {
86 if($checkCharacter == "*")
87 { $checkCharacter = "/"; }
88 else
89 {
90 $minifierString .= " ";
91 break;
92 }
93 }
94 $counter++;
95 }
96 }
97 else
98 {
99 $counter += 2;
100 while($counter < $contentSize) {
101 $character = $fileContent[$counter];
102 $code = intval(ord($character));
103 if($code == 10) {
104 $minifierString .= " ";
105 break;
106 }
107 $counter++;
108 }
109 }
110 }
111 else
112 { $minifierString .= $character; }
113 $counter++;
114 }
115 $outputFile = "minifier_".$this->fileName;
116 $fileObj = fopen($outputFile, "w+") or die("Unable to create/open file!");
117 fwrite($fileObj, $minifierString);
118 fclose($fileObj);
119 if(file_exists($outputFile))
120 { echo "Please find the minifier script in <b>[".$outputFile."]</b>"; }
121 else
122 { echo "Couldn't able to produce the output"; }
123 }
124 }
125 }
126 ?>

0 Comments

Leave a Comment:

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

Name *

Email *

Message *