本文章总结了几种利用php 清除换行符,清除制表符,去掉注释标记实现代码,有需要的朋友可参考,代码如下:
-
-
-
-
-
- function compress_html($string) {
- $string = str_replace("rn", '', $string);
- $string = str_replace("n", '', $string);
- $string = str_replace("t", '', $string);
- $pattern = array (
- "/> *([^ ]*) *</",
- "/[s]+/",
- "/<!--[^!]*-->/",
- "/" /",
- "/ "/",
- "'/*[^*]**/'"
- );
- $replace = array (
- ">\1<",
- " ",
- "",
- """,
- """,
- ""
- );
- return preg_replace($pattern, $replace, $string);
- }
去除连续的空格和换行符,代码如下:
- <?php
- $str="i am a booknnnnnmoth";
-
- echo preg_replace("/[s]{2,}/","",$str).'<br>';
-
- echo preg_replace("/([s]{2,})/","\1",$str);
- ?>
去除回车换行符:preg_replace("'([rn])[s]+'", "", $content) //去除回车换行符,代码如下:
- <?php
-
-
-
-
-
- $search = array ("'<script[^>]*?>.*?</script>'si",
- "'<[/!]*?[^<>]*?>'si",
- "'([rn])[s]+'",
- "'&(quot|#34);'i",
- "'&(amp|#38);'i",
- "'&(lt|#60);'i",
- "'&(gt|#62);'i",
- "'&(nbsp|#160);'i",
- "'&(iexcl|#161);'i",
- "'&(cent|#162);'i",
- "'&(pound|#163);'i",
- "'&(copy|#169);'i",
- "'&#(d+);'e");
-
- $replace = array ("",
- "",
- "\1",
- """,
- "&",
- "<",
- ">",
- " ",
- chr(161),
- chr(162),
- chr(163),
- chr(169),
- "chr(\1)");
-
- $text = preg_replace ($search, $replace, $document);
- ?>
|