-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCleaner.php
68 lines (59 loc) · 1.71 KB
/
Cleaner.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
<?php
namespace Depage\Html;
class Cleaner
{
public $dontCleanTags = array();
// {{{ constructor
/*
* constructor for cleaner, defines the tags of lines that should not
* be cleaned of whitespace e.g. pre and textarea
*/
public function __construct()
{
$this->dontCleanTags = implode("|<", array(
"pre",
"textarea",
));
}
// }}}
// {{{ clean()
/*
* Cleans the html of unnecessary spaces and empty lines
*
* @param $html html source to clean up
*
* @return $html cleaned html
*/
public function clean($html)
{
$htmlLines = explode("\n", $html);
$html = "";
$dontClean = 0;
foreach ($htmlLines as $i => $line) {
// check for opening tags
if ($m = preg_match_all("/<{$this->dontCleanTags}/", $line, $matches)) {
$dontClean += $m;
}
if ($dontClean > 0) {
// just copy the whole line
$html .= $line . "\n";
} else {
// trim line
$line = trim($line);
// replace multiple spaces with only one space
$line = preg_replace("/\"[^\"]*\"(*SKIP)(*FAIL)|'[^']*'(*SKIP)(*FAIL)|( )+/", " ", $line);
// throw away empty lines
if ($line != "") {
$html .= $line . "\n";
}
}
// check for closing tags
if ($m = preg_match_all("/<\/{$this->dontCleanTags}/", $line, $matches)) {
$dontClean -= $m;
}
}
return $html;
}
// }}}
}
/* vim:set ft=php sw=4 sts=4 fdm=marker et : */