PHP instance -- output Secure HTML code

Source: Internet
Author: User
//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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.