如题:php能不能够把html中的样式综合起来生成一个样式表文件?比如说:demo.html文件是下以内容<style>
.top1{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
</style>
<div class="top1">11111111111</div>
<style>
.top2{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
</style>
<div class="top2">2222222</div><style>
.top3{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
</style>
<div class="top3">3333333333</div>
能不能把样式属性综合在一起,并生成一个style.css 文件,如果可以该怎么做?
.top1{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
.top2{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
.top3{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
PHPHTML类
.top1{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
</style>
<div class="top1">11111111111</div>
<style>
.top2{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
</style>
<div class="top2">2222222</div><style>
.top3{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
</style>
<div class="top3">3333333333</div>
能不能把样式属性综合在一起,并生成一个style.css 文件,如果可以该怎么做?
.top1{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
.top2{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
.top3{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
PHPHTML类
解决方案 »
- linux 权限设置的问题
- 为什么这个脚本函数还能被调用呢?
- 在下刚刚接触PHP,请问各位大侠我应该如何搭建PHP环境?急!!!
- 关于postgresql,php的数据库连接资源
- 问一条 MYSQL 语句
- 关联表UPDATE问题
- 唠叨看看这样对不对?如果这样比较合理的话,我如何做到将这两个php文件作为整体分别assign到{header}和{footer}?
- 郁闷!Dreamweaver 模板问题。
- 参与解答--$_FILES的问题--的再来拿分啊!!!!!!!!!
- 请教:文件名称大小写问题....
- 求助,phpmyadmin和mysql控制台信息不一致。
- 数据修改成功后,页面显示的还是原来的数据
preg_match_all("@<style[^>]*?>(.*?)</style>@siu",$content,$match);
$style=join("\n",$match[1]);
$fp=fopen("style.css","w+");
fwrite($fp,$style);
fclose($fp);
demo.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<style>
.top1{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
</style>
<div class="top1">11111111111</div>
<style>
.top2{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
</style>
<div class="top2">2222222</div>
<style>
.top3{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
</style>
<div class="top3">3333333333</div>
</body>
</html>问题1,如果是上面哪样生成文件是空白的,如果是下面这样确能写文件,不能怎么调试?<style>
.top1{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
</style>
<style>
.top2{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
</style>
<style>
.top3{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
</style>
问题2,如果把demo.html中的样式提取出来生成了一个新的style.css文件,那个原有的demo.html中的样式如何删掉并重新生成一个demo.html没有样式的文件?
你的代码是如何写的?请贴出。问题2
$content=file_get_contents("demo.html");
$s= preg_replace("@<style[^>]*?>(.*?)</style>@siu",'',$content);
file_put_contents('demo.html',$s);
<?php
$s =<<< HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<style>
.top1{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
</style>
<div class="top1">11111111111</div>
<style>
.top2{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
</style>
<div class="top2">2222222</div>
<style>
.top3{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}
</style>
<div class="top3">3333333333</div>
</body>
</html>
HTML;$p = "#<style[^>]*?>(.*?)</style>#si";
preg_match_all($p, $s, $r);
echo join('', $r[1]);echo join('', preg_split($p, $s));提取出的样式
.top1{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}.top2{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}.top3{
margin:0px; padding:0px; font-size:12px; height:30px; line-height:30px;
}提取样式表后的 html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body><div class="top1">11111111111</div><div class="top2">2222222</div><div class="top3">3333333333</div>
</body>
</html>