如题: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

解决方案 »

  1.   

    把所有css代码写入一个css文件中,然后在html页面引入就可以了。
      

  2.   

    $content=file_get_contents("demo.html");
    preg_match_all("@<style[^>]*?>(.*?)</style>@siu",$content,$match);
    $style=join("\n",$match[1]);
    $fp=fopen("style.css","w+");
    fwrite($fp,$style);
    fclose($fp);
      

  3.   

    测试了一下,是生成了 style.css,但内容是空白的,
      

  4.   

    有二个问题请大神赐教一下:
    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没有样式的文件?
      

  5.   

    问题1
    你的代码是如何写的?请贴出。问题2
    $content=file_get_contents("demo.html");
    $s= preg_replace("@<style[^>]*?>(.*?)</style>@siu",'',$content);
    file_put_contents('demo.html',$s);
      

  6.   

    <xmp>
    <?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>