当前位置:Gxlcms > PHP教程 > PHP压缩html的函数代码

PHP压缩html的函数代码

时间:2021-07-01 10:21:17 帮助过:4人阅读

  1. //函数名: compress_html
  2. //参数: $string
  3. //返回值: 压缩后的$string
  4. //by bbs.it-home.org
  5. function compress_html($string) {
  6. $string = str_replace("\r\n", '', $string); //清除换行符
  7. $string = str_replace("\n", '', $string); //清除换行符
  8. $string = str_replace("\t", '', $string); //清除制表符
  9. $pattern = array (
  10. "/> *([^ ]*) * "/[\s]+/",
  11. "//",
  12. "/\" /",
  13. "/ \"/",
  14. "'/\*[^*]*\*/'"
  15. );
  16. $replace = array (
  17. ">\\1<",
  18. " ",
  19. "",
  20. "\"",
  21. "\"",
  22. ""
  23. );
  24. return preg_replace($pattern, $replace, $string);
  25. }

人气教程排行