Rumah >pembangunan bahagian belakang >tutorial php >php去掉从word直接粘贴过来的无用格式的函数

php去掉从word直接粘贴过来的无用格式的函数

WBOY
WBOYasal
2016-07-25 09:08:10781semak imbas
  1. function ClearHtml($content,$allowtags='') {

  2. mb_regex_encoding('UTF-8');

  3. //replace MS special characters first
  4. $search = array('/‘/u', '/’/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 to
  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 could be caught by strip_tags so that they won't be
  16. //'$content = preg_replace(array('/
  17. $content = strip_tags($content, $allowtags);

  18. //eliminate extraneous whitespace from start and end of line, or anywhere there are two or more spaces, convert it to one
  19. $content = preg_replace(array('/^\s\s+/', '/\s\s+$/', '/\s\s+/u'), array('', '', ' '), $content);
  20. //strip out inline css and simplify style tags
  21. $search = array('#]*>(.*?)(strong|b)>#isu', '#]*>(.*?)(em|i)>#isu', '#]*>(.*?)#isu');
  22. $replace = array('$2', '$2', '$1');
  23. $content = preg_replace($search, $replace, $content);
  24. //on some of the ?newer MS Word exports, where you get conditionals of the form 'if gte mso 9', etc., it appears

  25. //that whatever is in one of the html comments prevents strip_tags from eradicating the html comment that contains
  26. //some MS Style Definitions - this last bit gets rid of any leftover comments */
  27. $num_matches = preg_match_all("/\
  28. 《优伴户外旅行》——让旅行成为习惯!

    越发忙碌的你,是否想给自己放个假?专注工作的你,是否还记得上一次锻炼是什么时候?优伴户外旅行,给你不一样的旅行体验:给心自由,便处处都是风景!';
  29. echo ClearHtml($content,'

    ');

  30. /*

  31. 得到的结果:
  32. 《优伴户外旅行》--让旅行成为习惯!

    越发忙碌的你,是否想给自己放个假?专注工作的你,是否还记得上一次锻炼是什么时候?优伴户外旅行,给你不一样的旅行体验:给心自由,便处处都是风景!
  33. */
  34. ?>
复制代码


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn