Small functions that remove HTML tags and javascript code

Source: Internet
Author: User
Small functions that remove HTML tags and javascript code

  1. Function trip_html ($ html, $ len ){
  2. // $ Html should contain an HTML document.
  3. // In this example, the HTML tag and javascript code are removed.
  4. // And blank characters. Some common
  5. // Convert the HTML object to the corresponding text.
  6. $ Search = array ("' ] *?>. *? Script 'Si ", // remove javascript
  7. '<[\/\!] *? [^ <>] *?> 'Si ", // remove the HTML tag
  8. "'([\ R \ n]) [\ s] +'", // remove the white space
  9. '& (Quot | #34);' I ", // replaces the HTML object
  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 (1, 161 ),
  28. Chr (1, 162 ),
  29. Chr (1, 163 ),
  30. Chr (1, 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. ?>

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.