gpt4 book ai didi

php - 如何即时缩小CSS?

转载 作者:太空宇宙 更新时间:2023-11-04 14:12:37 26 4
gpt4 key购买 nike

我正在尝试在 WordPress 中即时缩小 css,因为如果我直接进行缩小,那么当评论被删除时,我的主题将无法工作。

首先,我尝试从任何指定的 css 文件中删除注释,这里的代码工作正常

<?php
$cssfile = 'style.css';//css file needed to minify
$file = fopen($cssfile, 'r');
if ($file) {
$buffer = file_get_contents('style.css');//getting content of css file needed to minify
$buffers = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);

echo $buffers;
}?>

我想即时做同样的事情。在很多教程中都使用了ob_start() 和关于http request 的说法。我不知道这些东西以及它们的作用,而且我无法看到它们在哪里指定文件路径,就像我在第一行 $cssfile

中所做的那样

我认为他们正在告诉 $_GET[something] 文件。这是他们提到需要缩小的 css 文件的地方吗?(提到 css 的路径)

任何人都可以帮助我做一个动态 css 缩小并解释 ob_start()、http 请求及其工作原理。

最佳答案

可以从 css 文件中删除多余的空格

try this

<?php
$search = array(
'/\>[^\S]+/s', // strip whitespaces after tags, except space
'/[^\S]+\</s', // strip whitespaces before tags, except space
'/(\s)+/s' // shorten multiple whitespace sequences
);

$replace = array(
'>',
'<',
'\\1'
);

$style = preg_replace($search, $replace, $buffer = file_get_contents('style.css'));
?>

<style type="text/css"><?php echo $style; ?></style>

关于php - 如何即时缩小CSS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20627262/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com