国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
841B

  1. <?php
  2. if (!defined('DEDEINC')) exit ('dedebiz');
  3. require_once DEDEINC."/libraries/HTMLPurifier/HTMLPurifier.auto.php";
  4. function SpHtml2Text($html)
  5. {
  6. // 初始化 HTMLPurifier 配置
  7. static $purifier = null;
  8. if ($purifier === null) {
  9. $config = HTMLPurifier_Config::createDefault();
  10. // 禁止所有 HTML 标签,只允许文本
  11. $config->set('HTML.Allowed', '');
  12. // 配置缓存
  13. $cacheDir = DEDEDATA.'/cache';
  14. $config->set('Cache.SerializerPath', $cacheDir);
  15. $purifier = new HTMLPurifier($config);
  16. }
  17. // 过滤掉所有 HTML,只保留纯文本
  18. $cleanText = $purifier->purify($html);
  19. // 进一步去除可能的额外空格和换行符
  20. $cleanText = trim($cleanText);
  21. $cleanText = preg_replace("/[\r\n\t ]+/", ' ', $cleanText);
  22. return $cleanText;
  23. }
  24. ?>