HtmlCleaner

 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
function print_html_clean($html,$tags=array()) {
global $cfg;
include_once($cfg['dir_adm'].'/lib/php/HtmlCleaner/cleanhtml.class.php');
$xhtml=new HtmlCleaner($html);
if (is_array($tags) && count($tags))
$xhtml->allowedTags($tags);
else
$xhtml->allowedTags(array("<table>","<th>","<td>","<tr>","<tbody>","<thead>","<p>","<ul>","<ol>","<li>"));
return $xhtml->GetCleanedHtml();
}
class HtmlCleaner {
var $lt="%#:";
var $gt=":#%";
var $html="";
var $cleanedHtml="";
var $allowedtags=array("table","p","td","tr","th","tbody","thead");
function GetCleanedHtml() {
$this->cleanedHtml=$this->html;
for ($i=0;$i<count($this->allowedtags);$i++) {
//arrays for fase 1 cleaning
$pseudotags[]=$this->lt.$this->allowedtags[$i].$this->gt."<";
$pseudotags[]=$this->lt.$this->allowedtags[$i].$this->gt."<";
$pseudotags[]=$this->lt."/".$this->allowedtags[$i].$this->gt;
$originaltags[]="<".$this->allowedtags[$i]." ";
$originaltags[]="<".$this->allowedtags[$i];
$originaltags[]="</".$this->allowedtags[$i].">";
//arrays for fase 2 cleaning
$pseudotags2[]=$this->lt.$this->allowedtags[$i].$this->gt;
$pseudotags2[]=$this->lt."/".$this->allowedtags[$i].$this->gt;
$originaltags2[]="<".$this->allowedtags[$i].">";
$originaltags2[]="</".$this->allowedtags[$i].">";
//remove empty tags like <p></p>
$emptytags1[]="<".$this->allowedtags[$i].">"."</".$this->allowedtags[$i].">";
$emptytags2[]="";
}
//main cleaning utility
$this->cleanedHtml=str_replace($originaltags,$pseudotags,$this->cleanedHtml);
$this->cleanedHtml=strip_tags($this->cleanedHtml);
$this->cleanedHtml=str_replace($pseudotags2,$originaltags2,$this->cleanedHtml);
$this->cleanedHtml=preg_replace("/\r\n/","",$this->cleanedHtml);
$this->cleanedHtml=preg_replace("/\t/"," ",$this->cleanedHtml);
$this->cleanedHtml=preg_replace("/ /"," ",$this->cleanedHtml);
$this->cleanedHtml=preg_replace("/> *</","><",$this->cleanedHtml);
return $this->cleanedHtml;
}
function allowedTags($tags) {
$this->allowedtags=array();
for ($i=0;$i<count($tags);$i++) {
$tag=str_replace(array("<",">","/>"),array("","",""),$tags[$i]);
$this->allowedtags[]=$tag;
}
}
}