php怎样删除css文件中的多余空白?body{
font-size:12px;
font-weight:bold;
}
.good{ color:red;}
.bad{color:green;}
.hello{background-color:yellow;}.world{float:left};
font-size:12px;
font-weight:bold;
}
.good{ color:red;}
.bad{color:green;}
.hello{background-color:yellow;}.world{float:left};
$str = <<<eof
body{
font-size:12px;
font-weight:bold;
}
.good{ dolor:red;}
.bad{color:green:}
.hell{background-color:yellow;}.world{float:left};
eof;
$f = array("/\s+/","/\./");
$t = array("","\n.");
echo preg_replace($f,$t,$str);输出:
body{font-size:12px;font-weight:bold;}
.good{dolor:red;}
.bad{color:green:}
.hell{background-color:yellow;}
.world{float:left};
$t = array("","\n.");
echo preg_replace($f,$t,$str);
数组替换。
我是这样想的<?php
$str = <<<eof
body{
font-size:12px;
font-weight:bold;
}
.good{ dolor:red;}
.bad{color:green:}
.hell{background-color:yellow;}.world{float:left};
eof;
for($i=0;$i<strlen($str);$i++){
if($str[$i]!=' ')
echo $str[$i];
};
?>
/*
* @Date 2011-2-26
* @Author h2ero
* Email [email protected]
* Blog blog.h2ero.cn
*/
$str=<<<eof
body{
font-size:12px;
font-weight:bold;
}
.nav ul.user{float:right;margin:
15px 5px 0 0;font-weight:
bold;}
.nav ul.user li{color:#000;
font-weight:normal;}.nav ul.user li a{color:#000;
padding:0 6px;}
.nav ul.user #vName{font-weight:bold;color:#000;}
.nav ul.user input.btn{margin:0 3px -5px 0;}
eof;function replaceblank($s){
$s=$s[1];
$search=array('/([\r\n])[\s]+/');
$replace=array('');
$result=preg_replace($search,$replace,$s);
$result="{"."$result"."}";
return $result;
}$pattern='/\{(.*?)\}/is';
$str=preg_replace_callback($pattern,replaceblank,$str);//先删除{}的空格和换行
$str=preg_replace('/\r\n/','\\1',$str);//去除{}外边的换行,因为可能有多个所以删了再添加下
$replace="\\1}".chr(13);
$str=preg_replace('/}/',$replace,$str);//在}后添加换行
$pattern='/(.*?)[\r\n]/is';
preg_match_all($pattern,$str,$result);//逐行读出在去掉空格换行
for($i=0;$i<strlen($result[0]);$i++)
{
$result[0][$i]=trim($result[0][$i]);
$resultstring.=$result[0][$i].chr(13);//再添加换行
}
echo "<pre>";
print_r($resultstring);
?>格式化后的样子:body{font-size:12px;font-weight:bold;}
.nav ul.user{float:right;margin:15px 5px 0 0;font-weight:bold;}
.nav ul.user li{color:#000;font-weight:normal;}
.nav ul.user li a{color:#000;padding:0 6px;}
.nav ul.user #vName{font-weight:bold;color:#000;}