//Output Secure HTML
FunctionH ($ Text,$ Tags=Null){
$ Text=Trim($ Text);
//Completely filter comments
$ Text=Preg_replace('/<! --?. * --> /','',$ Text);
//Completely filter Dynamic DataCode
$ Text=Preg_replace('/<\? | \? '.'> /','',$ Text);
//Completely filter JS
$ Text=Preg_replace('/<Script ?. * \/SCRIPT> /','',$ Text);
$ Text=Str_replace('[', '& #091 ;',$ Text);
$ Text=Str_replace(']', '& #093 ;',$ Text);
$ Text=Str_replace('|', '& #124 ;',$ Text);
//Filter line breaks
$ Text=Preg_replace('/\ R? \ N /','',$ Text);
//BR
$ Text=Preg_replace('/<Br (\ s \/)? '.'>/I ',' [br] ',$ Text);
$ Text=Preg_replace('/(\ [Br \] \ s *) {10,}/I', '[br]',$ Text);
//Filter dangerous attributes, such as: Filter on Event Lang JS
While(Preg_match('/(<[^> <] +) (Lang | on | action | background | codebase | DYNSRC | lowsrc) [^> <] +/I ',$ Text,$ Mat)){
$ Text=Str_replace($ Mat[0],$ Mat[1],$ Text);
}
While(Preg_match('/(<[^> <] +) (Window \. | javascript: | JS: | about: | file: | document \. | vbs: | cookie) ([^> <] *)/I ',$ Text,$ Mat)){
$ Text=Str_replace($ Mat[0],$ Mat[1].$ Mat[3],$ Text);
}
If(Empty($ Tags)){
$ Tags= 'Table | TD | th | tr | I | B | u | strong | IMG | p | BR | Div | strong | em | ul | ol | Li | DL | dd | dT | ';
}
//Allowed HTML tags
$ Text=Preg_replace('/<('.$ Tags. ') ([^> <\ [\] *)>/I', '[\ 1 \ 2]',$ Text);
//Filter excess html
$ Text=Preg_replace('/<\/? (HTML | HEAD | meta | link | base | basefont | body | bgsound | title | style | script | form | IFRAME | frame | frameset | applet | ID | ilayer | layer | Name | script | style | XML) [^> <] *>/I ','',$ Text);
//Filter valid HTML tags
While(Preg_match('/<([A-Z] +) [^> <\ [\] *> [^> <] * <\/\ 1>/I ',$ Text,$ Mat)){
$ Text=Str_replace($ Mat[0],Str_replace('>', ']',Str_replace('<','[',$ Mat[0]),$ Text);
}
//Conversion quotation marks
While(Preg_match('/(\ [^ \ [\] * = \ S *) (\ "| \') ([^ \ 2 = \ [\] +) \ 2 ([^ \ [\] * \])/I ',$ Text,$ Mat)){
$ Text=Str_replace($ Mat[0],$ Mat[1]. '| '.$ Mat[3]. '| '.$ Mat[4],$ Text);
}
//Filter Single quotation marks with errors
While(Preg_match('/\ [[^ \ [\] * (\ "| \') [^ \ [\] * \]/I ',$ Text,$ Mat)){
$ Text=Str_replace($ Mat[0],Str_replace($ Mat[1], '',$ Mat[0]),$ Text);
}
//Convert all other illegal <>
$ Text=Str_replace('<', '& Lt ;',$ Text);
$ Text=Str_replace('>', '& Gt ;',$ Text);
$ Text=Str_replace('"', '& Quot ;',$ Text);
//Reverse Conversion
$ Text=Str_replace('[', '<',$ Text);
$ Text=Str_replace(']', '> ',$ Text);
$ Text=Str_replace('| ','"',$ Text);
//Filter extra spaces
$ Text=Str_replace('','',$ Text);
Return $ Text;
}