-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMinify.php
More file actions
78 lines (69 loc) · 1.86 KB
/
Copy pathMinify.php
File metadata and controls
78 lines (69 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* Created by PhpStorm.
* User: orion
* Date: 14/12/2018
* Time: 21:56
*/
class Minify
{
private static function removeComment($s) {
// remove line-comment
$lines = explode("\n", $s);
//error_log('removing comment');
for ($i = 0; $i < count($lines); $i++) {
$squoting = false;
$dquoting = false;
$lastchar = '';
for ($j = 0; $j < strlen($lines[$i]); $j++) {
$char = $lines[$i][$j];
if ($char == '\'') {
$squoting = !$squoting;
}
if ($char == '"') {
$dquoting = !$dquoting;
}
if (!$squoting && !$dquoting) {
if ($char == '/' && $lastchar == '/') {
$lines[$i] = substr($lines[$i], 0, $j - 1);
break;
}
}
$lastchar = $char;
}
}
$s = implode("\n", $lines);
// remove multi-line comment
$s = preg_replace("/\/\*([\s\S]*?)\*\//", '', $s);
return $s;
}
/**
* Remove all white space, except for after var statement.
* Also remove comments.
* @param string $s e.g. javascript
* @return string
*/
public static function process($s) {
//
$s = self::removeComment($s);
// escape
$search = array(
'var ',
'return ',
'&',
'"',
'function ',
);
$replace = array(
'var%20',
'return%20',
'&',
'"',
'function%20',
);
$s = str_replace($search, $replace, $s);
// replace white space to single spaces
$s = preg_replace('/\s+/', ' ', $s);
return $s;
}
}