-
Notifications
You must be signed in to change notification settings - Fork 2
/
step_three.php
78 lines (56 loc) · 2.52 KB
/
step_three.php
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
<?php
session_start();
require_once 'simple_html_dom.php';
require_once 'css_parser.php';
set_time_limit(90);
$data = array();
$do_not_remove_items = array();
if(isset($_POST['do_not_remove_items'])) {
$do_not_remove_items = $_POST['do_not_remove_items'];
}
$unused = $_SESSION["unused"];
$css_string = $_SESSION["css_string"];
//remove $do_not_remove_items from $unused i;e treat it as though used
if(is_array($do_not_remove_items) and !empty($do_not_remove_items)){
foreach($do_not_remove_items as $item){
unset($unused[$item]);
}
$unused = array_values($unused); // 'reindex' array
}
usort($unused,'compareStrLengthNegatively'); // because we want the longer elements to be removed first
function compareStrLengthNegatively($a,$b){
return strlen($b)-strlen($a);
}
// Now the css generation stuff
foreach($unused as $unuseditem) {
$unuseditem = preg_quote($unuseditem, '/');
$unuseditem = '(?:(?<=^|\s)(?=\S|$)|(?<=^|\S)(?=\s|$))'.$unuseditem.'(?:(?<=^|\s)(?=\S|$)|(?<=^|\S)(?=\s|$)) *{';
$css_string = preg_replace('/'.$unuseditem.'/', "{", $css_string);
}
foreach($unused as $unuseditem) {
$unuseditem = preg_quote($unuseditem, '/');
$unuseditem = '(?:(?<=^|\s)(?=\S|$)|(?<=^|\S)(?=\s|$))'.$unuseditem.'(?:(?<=^|\s)(?=\S|$)|(?<=^|\S)(?=\s|$)) *,';
$css_string = preg_replace('/'.$unuseditem.'/', ",", $css_string);
}
$css_string = preg_replace("/(,\s*){2,}/", ",", $css_string); // remove multiple instances of comma
$css_string = preg_replace("/}\s*?(,|>)/", "}", $css_string); // remove deinitions with only comma or > left as selector
do {
$css_string = preg_replace('/}\s*,?\s*{[^}]*}/S', "}", $css_string, -1, $count); //remove definitions with no selector elements
} while ($count);
do {
$css_string = preg_replace('/{\s*,?\s*{[^}]*}/S', "{", $css_string, -1, $count);// handle 1st unused definition within media query format like ' { {some definitions here }'
} while ($count);
$css_string = preg_replace("/,\s*{/", "{", $css_string); //remove instances like ', {'
//$css_string = preg_replace("/{\s*,/", "{", $css_string); //remove instances like '{ ,'
$css_string = str_replace(' , ', ',', $css_string); //
$css_string = str_replace('}',"}<br>", $css_string);
$css_string = preg_replace('/\s+/', ' ', $css_string); // remove excess white spaces
//houston we are ready;
$data['content'] = $css_string ;
$data['success']= true;
echo json_encode($data);
// remove all session variables
session_unset();
// destroy the session
session_destroy();
?>