您知道有哪些 php 函数可以动态美化/格式化内联 css 吗?
我有 php 函数,可以执行相反的操作并缩小来自 https://gist.github.com/tovic/d7b310dea3b33e4732c0 的 css、js、html
我正在寻找至少对 css 起到相反作用的东西,希望对 js 也是如此。
我在我正在构建的主题中使用这个缩小功能,但我想为开发人员提供加载非缩小样式和脚本以进行测试和开发的选项。只需加载 .css 文件而不是 .min.css 文件,这已经对我有用了。但是,对于某些内联样式,这不起作用。当开发人员选择加载美化文件时,我希望内联(回声)样式也能得到美化。这太好了!
你知道吗?
谢谢。
如果有人在 2024 年仍然需要这样做,您可以在如下函数中使用 preg_replace:
private static function prettify_CSS($css) {
$css = trim($css);
$css = preg_replace('/\s+/', ' ', $css); // Remove any existing whitespace and line breaks
$css = preg_replace('/\s*{\s*/', " {\n ", $css); // Add newlines and indentation
$css = preg_replace('/;\s*/', ";\n ", $css);
$css = preg_replace('/\s*}\s*/', "\n}\n\n", $css);
$css = preg_replace('/;\n\s*}/', ";\n}", $css); // Fixing the indentation for the closing brace
$css = preg_replace("/\n\s*\n/", "\n\n", $css); // Removing any extra newlines
$css = trim($css); // Trimming
return $css;
}
这适用于简单的 CSS 文件,但不适用于 @media 查询或 SCSS 等嵌套的内容。为此,您可能需要逐行执行此操作,并跟踪缩进。