Improving PHP Code Efficiency with Minification

PHP Minifier is a tool or process that reduces the size of PHP code by removing unnecessary characters, such as whitespaces, comments, and line breaks, without affecting its functionality. The purpose of minifying PHP code is to improve page load times and reduce bandwidth usage by reducing the overall file size.Minification is commonly used in web development to optimize the delivery of code to the client's browser. By removing unnecessary characters, the file size is reduced, resulting in faster downloads and improved performance.

You can get the complete code from Github

PHP minifiers typically operate by parsing the PHP code and removing any characters that are not essential for the code's execution. This includes removing whitespace, comments, and unnecessary line breaks. However, it's important to note that minification should not alter the logic or functionality of the code.
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 ?>


index.php
1 <style>
2     body {
3     background-color:#0C134F;
4     color:#9BA4B5;
5     }
6 </style>
7 <?php
8 include_once("PHPMinifier.php");
9 $obj = new PHPMinifier();
10 $obj->apply("PHPMinifier.php");
11 $obj->apply("PHPMinifier.php");
12 ?>

No comments:

Post a Comment