首页  >  文章  >  后端开发  >  PHP压缩html的函数代码

PHP压缩html的函数代码

WBOY
WBOY原创
2016-07-25 08:55:32936浏览
  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. "/> *([^ ]*) *", //去掉注释标记
  11. "/[\s]+/",
  12. "//",
  13. "/\" /",
  14. "/ \"/",
  15. "'/\*[^*]*\*/'"
  16. );
  17. $replace = array (
  18. ">\\1 " ",
  19. "",
  20. "\"",
  21. "\"",
  22. ""
  23. );
  24. return preg_replace($pattern, $replace, $string);
  25. }
复制代码


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn