<span>_border-radius: 10px;</span>该代码将生成这样的属性列表:
<span>-o-border-radius: 10px; </span><span>-moz-border-radius: 10px; </span><span>-webkit-border-radius: 10px; </span><span>border-radius: 10px;</span>然后,在HTML中,以这样的方式编写了一个链接以导入样式:
<span><span><span><link</span> rel<span>="stylesheet"</span> href<span>="css/css.php?f=css_file1|css_file2|css_file3"</span>></span></span>使用单个链接元素,将将三个CSS文件加载为一个。 CSS.PHP脚本将在列出的文件中读取(css_file1.css,css_file2.css和css_file3.css),组合它们并将其返回为单个文件。 看起来很容易使用,对吗?因此,有了进一步的ADO,让我们开始编写一些代码!
<?php $files = explode("|", $_GET["f"]); $contents = ""; foreach ($files as $file) { $contents .= file_get_contents($file . ".css"); } preg_match_all('/_[a-zA-Z-]+:s.+;|[a-zA-Z-]+:s_[a-zA-Z].+;/', $contents, $matches, PREG_PATTERN_ORDER); $prefixes = array("-o-", "-moz-", "-webkit-", ""); foreach ($matches[0] as $property) { $result = ""; foreach ($prefixes as $prefix) { $result .= str_replace("_", $prefix, $property); } $contents = str_replace($property, $result, $contents); } $contents = preg_replace('/(/*).*?(*/)/s', '', $contents); $contents = preg_replace(array('/s+([^w'"]+)s+/', '/([^w'"])s+/'), '', $contents); header("Content-Type: text/css"); header("Expires: " . gmdate('D, d M Y H:i:s GMT', time() + 3600)); echo $contents;该代码首先接收到要从URL参数中处理为字符串的CSS文件列表(在PHP中可访问为$ _GET [“ F”])。每个文件都用管道形式分开。 Explode()函数将返回文件名数组的管道上的字符串拆分。 功能file_get_contents() 将每个文件的内容接一个地添加到变量$内容上。 在检索了CSS文件的内容后,下一步是找到以下划线开头的任何CSS属性,并用特定于浏览器的前缀属性替换它们。函数preg_match_all()找到匹配正则表达式的文本中的所有零件,并将匹配项放入$匹配[0]作为数组中。 我不会解释为什么$ Matches具有数组索引0,因为您可以阅读有关PHP手册中该功能的明确说明。相反,我想专注于解释我们计划的流程。 此图像解释了正则表达式的模式:
<span>_border-radius: 10px;</span>第二个文件是Center.css:
<span>-o-border-radius: 10px; </span><span>-moz-border-radius: 10px; </span><span>-webkit-border-radius: 10px; </span><span>border-radius: 10px;</span>第三个文件是页脚:css:
<span><span><span><link</span> rel<span>="stylesheet"</span> href<span>="css/css.php?f=css_file1|css_file2|css_file3"</span>></span></span>看看如何编写CSS3属性;那些具有特定于浏览器的前缀的人只给出了一次,并且在它们面前有一个下划线。 接下来,创建将使用样式的文件索引索引。
<?php $files = explode("|", $_GET["f"]); $contents = ""; foreach ($files as $file) { $contents .= file_get_contents($file . ".css"); } preg_match_all('/_[a-zA-Z-]+:s.+;|[a-zA-Z-]+:s_[a-zA-Z].+;/', $contents, $matches, PREG_PATTERN_ORDER); $prefixes = array("-o-", "-moz-", "-webkit-", ""); foreach ($matches[0] as $property) { $result = ""; foreach ($prefixes as $prefix) { $result .= str_replace("_", $prefix, $property); } $contents = str_replace($property, $result, $contents); } $contents = preg_replace('/(/*).*?(*/)/s', '', $contents); $contents = preg_replace(array('/s+([^w'"]+)s+/', '/([^w'"])s+/'), '', $contents); header("Content-Type: text/css"); header("Expires: " . gmdate('D, d M Y H:i:s GMT', time() + 3600)); echo $contents;查看链接标签中的HREF属性。每个CSS文件名都被管道隔开。
>如何使用CSS3前缀和Compressor工作?
a css3 prefixer和压缩机通过扫描CSS for properties for Properies for Properies prefix verefix verefix vendor.然后,它会自动添加这些前缀,从而节省您手动进行的时间和精力。压缩机功能通过删除CSS文件中的空格,评论和线路断裂等不必要的字符来起作用,从而降低了它们的尺寸。>为什么我要使用CSS3 prefixer和压缩机可以很好地使用CSS3前缀和压缩机?它确保您的CSS属性在不同的浏览器上工作,并减少CSS文件的大小,从而提高网站的加载速度。它还为您节省了手动添加供应商前缀和压缩CSS文件的时间和精力。>
>
>>>,而CSS3 prefixer和Compressor提供了许多好处,重要的是要注意,可能并不总是必要的。一些现代浏览器不再需要某些CSS属性的供应商前缀。另外,过度压缩您的CSS文件可能会使它们难以读取和维护。
>有哪些良好的CSS3前缀和压缩机工具?
>
有许多良好的CSS3 prefixer和Compressor工具可用,包括AutoPrefixer,PostCSS和CSS驱动器。这些工具提供了一系列功能,可以与各种CSS预处理器一起使用。以上是自动CSS3前缀和压缩机的详细内容。更多信息请关注PHP中文网其他相关文章!