Php removes useless format functions directly pasted from word

Source: Internet
Author: User
Tags html comment
Php removes useless format functions directly pasted from word

  1. Function ClearHtml ($ content, $ allowtags = ''){

  2. Mb_regex_encoding ('utf-8 ');

  3. // Replace MS special characters first
  4. $ Search = array ('/U','/U', '/', '/"/U ', '/-/u ');
  5. $ Replace = array ('\'','\'','"','"','-');
  6. $ Content = preg_replace ($ search, $ replace, $ content );
  7. // Make sure _ all _ html entities are converted to the plain ascii equivalents-it appears
  8. // In some MS headers, some html entities are encoded and some aren't
  9. $ Content = html_entity_decode ($ content, ENT_QUOTES, 'utf-8 ');
  10. // Try to strip out any C style comments first, since these, embedded in html comments, seem
  11. // Prevent strip_tags from removing html comments (MS Word introduced combination)
  12. If (mb_stripos ($ content ,'/*')! = FALSE ){
  13. $ Content = mb_eregi_replace ('#/\*.*? \ */# S', '', $ content, 'M ');
  14. }
  15. // Introduce a space into any arithmetic expressions that cocould be caught by strip_tags so that they won't be
  16. // '<1' becomes' <1' (note: somewhat application specific)
  17. $ Content = preg_replace (array ('/<([0-9] +)/'), array ('<$1'), $ content );

  18. $ Content = strip_tags ($ content, $ allowtags );

  19. // Eliminate extraneous whitespace from start and end of line, or anywhere there are two or more spaces, convert it to one
  20. $ Content = preg_replace (array ('/^ \ s +/', '/\ s + $ /', '/\ s +/u'), array ('','', ''), $ content );
  21. // Strip out inline css and simplify style tags
  22. $ Search = array ('# <(strong | B) [^>] *> (.*?) # Isu', '# <(em | I) [^>] *> (.*?) # Isu ','# ] *> (.*?)# Isu ');
  23. $ Replace = array ('$2','$2','$1');
  24. $ Content = preg_replace ($ search, $ replace, $ content );

  25. // On some of? Newer MS Word exports, where you get conditionals of the form 'If gte mso 9', etc., it appears

  26. // That whatever is in one of the html comments prevents strip_tags from eradicating the html comment that contains
  27. // Some MS Style Definitions-this last bit gets rid of any leftover comments */
  28. $ Num_matches = preg_match_all ("/\
  29. Companion outdoor travel-making travel a habit!

    If you are busy, do you want to give yourself a vacation? Do you still remember when the last exercise was? When traveling outdoors, you can enjoy a different travel experience: If you are at your freedom, you can see scenery everywhere!

    ';
  30. Echo ClearHtml ($ content ,'

    ');

  31. /*

  32. Result:
  33. Companion outdoor travel-making travel a habit!

    If you are busy, do you want to give yourself a vacation? Do you still remember when the last exercise was? When traveling outdoors, you can enjoy a different travel experience: If you are at your freedom, you can see scenery everywhere!

  34. */
  35. ?>

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.