Heim  >  Artikel  >  Backend-Entwicklung  >  php压缩html网页代码(清除空格、换行符、制表符、注释标记等)的方法

php压缩html网页代码(清除空格、换行符、制表符、注释标记等)的方法

WBOY
WBOYOriginal
2016-07-25 09:05:49957Durchsuche
  1. /**
  2. * 压缩html : 清除换行符,清除制表符,去掉注释标记
  3. * @param $string
  4. * @return 压缩后的$string
  5. * from: bbs.it-home.org
  6. * */
  7. function compress_html($string) {
  8. $string = str_replace("\r\n", '', $string); //清除换行符
  9. $string = str_replace("\n", '', $string); //清除换行符
  10. $string = str_replace("\t", '', $string); //清除制表符
  11. $pattern = array (
  12. "/> *([^ ]*) *", //去掉注释标记
  13. "/[\s]+/",
  14. "//",
  15. "/\" /",
  16. "/ \"/",
  17. "'/\*[^*]*\*/'"
  18. );
  19. $replace = array (
  20. ">\\1" ",
  21. "",
  22. "\"",
  23. "\"",
  24. ""
  25. );
  26. return preg_replace($pattern, $replace, $string);
  27. }
  28. ?>
复制代码

>>> 您可能感兴趣的文章: php去除字符串换行符实例解析 php压缩html(清除换行符,清除制表符,去掉注释标记) php表单中转换textarea换行符的方法 php正则过滤html标签、空格、换行符等的代码示例 php去除换行符的方法小结 提供几个php替换换行符的方法 php生成excel与控制Excel单元格中的换行符



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn