Home > Backend Development > PHP Tutorial > Small function to remove HTML tags and javascript code

Small function to remove HTML tags and javascript code

WBOY
Release: 2016-07-25 09:02:48
Original
884 people have browsed it
  1. function trip_html( $html, $len ) {
  2. // $html should contain an HTML document.
  3. // This example will remove HTML tags, javascript code
  4. // and whitespace characters. Some common
  5. // HTML entities will also be converted into corresponding text.
  6. $search = array (“']*?>.*?'si”, // Remove javascript
  7. “'<[/!]*?[^< ;>]*?>'si", // Remove HTML tags
  8. "'([rn])[s]+'", // Remove whitespace characters
  9. "'&(quot|#34);'i ", // Replace HTML entity
  10. "'&(amp|#38);'i",
  11. "'&(lt|#60);'i",
  12. "'&(gt|#62);'i ",
  13. "'&(nbsp|#160);'i",
  14. "'&(iexcl|#161);'i",
  15. "'&(cent|#162);'i",
  16. "' &(pound|#163);'i",
  17. "'&(copy|#169);'i",
  18. "'(d+);'e"); // Run as PHP code
  19. $replace = array ("",
  20. "",
  21. "\1",
  22. """,
  23. "&",
  24. "<",
  25. ">",
  26. " ",
  27. chr(161),
  28. chr (162),
  29. chr(163),
  30. chr(169),
  31. "chr(\1)");
  32. $text = preg_replace ($search, $replace, $html);
  33. $text = trim($text );
  34. return mb_strlen($text) >= $len ? mb_substr($text, 0, $len) : ”;
  35. }
  36. ?>
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template