Php 替换@import和@import之间的文本\n
我使用PHP 我正在研究一种方法,可以自动将我所有的CSS文件放在一起。我自动加载CSS文件,然后将它们保存到一个更大的文件中,以便上传 在我的本地安装中,有一些@import行需要删除 看起来是这样的:Php 替换@import和@import之间的文本\n,php,css,stylesheet,preg-replace,preg-match,Php,Css,Stylesheet,Preg Replace,Preg Match,我使用PHP 我正在研究一种方法,可以自动将我所有的CSS文件放在一起。我自动加载CSS文件,然后将它们保存到一个更大的文件中,以便上传 在我的本地安装中,有一些@import行需要删除 看起来是这样的: @import url('css/reset.css'); @import url('css/grid.css'); @import url('css/default.css'); @import url('css/header.css'); @import url('css/main.css
@import url('css/reset.css');
@import url('css/grid.css');
@import url('css/default.css');
@import url('css/header.css');
@import url('css/main.css');
@import url('css/sidebar.css');
@import url('css/footer.css');
body { font: normal 0.75em/1.5em Verdana; color: #333; }
如果上面的样式在一个字符串中,如何用preg_replace或更好的替换@import行?最好不要留下空白 str_替换(“@import”,”$str) 您可以轻松地迭代每一行,然后确定它是否以@import开头
$handle = @fopen('/path/to/file.css', 'r');
if ($handle) {
while (!feof($handle)) {
$line = fgets($handle, 4096);
if (strpos($line, '@import') !== false) {
// @import found, skip over line
continue;
}
echo $line;
}
fclose($handle);
}
或者,如果要将文件预先存储在阵列中:
$lines = file('/path/to/file.css');
foreach ($lines as $num => $line) {
if (strpos($line, '@import') !== false) {
// @import found, skip over line
continue;
}
}
您可以轻松地迭代每一行,然后确定它是否以@import开头
$handle = @fopen('/path/to/file.css', 'r');
if ($handle) {
while (!feof($handle)) {
$line = fgets($handle, 4096);
if (strpos($line, '@import') !== false) {
// @import found, skip over line
continue;
}
echo $line;
}
fclose($handle);
}
或者,如果要将文件预先存储在阵列中:
$lines = file('/path/to/file.css');
foreach ($lines as $num => $line) {
if (strpos($line, '@import') !== false) {
// @import found, skip over line
continue;
}
}
使用preg_match查找@imports可能更容易,然后使用str_replace替换它们
$str = "<<css data>>";
while (preg_match("/@import\s+url\('([^']+)'\);\s+/", $str, $matches)) {
$url = $matches[1];
$text = file_get_contents($url); // or some other way of reading that url
$str = str_replace($matches[0], $text, $str);
}
应该完成这项工作…使用preg\u match查找@imports可能更容易,然后使用str\u replace替换它们
$str = "<<css data>>";
while (preg_match("/@import\s+url\('([^']+)'\);\s+/", $str, $matches)) {
$url = $matches[1];
$text = file_get_contents($url); // or some other way of reading that url
$str = str_replace($matches[0], $text, $str);
}
应该完成工作…这应该通过正则表达式处理:
preg_replace('/\s*@import.*;\s*/iU', '', $text);
这应该通过正则表达式处理:
preg_replace('/\s*@import.*;\s*/iU', '', $text);
这会起作用,但感觉不是解决问题的最佳方法。如果我没有找到更好的方法,我可能会使用这个。Regex很慢,这将允许您在线性时间内创建一个新文件,假设您在迭代每个文件时创建输出。regexp是否更慢?因为我只在本地主机上生成CSS文件,所以速度对我来说并不重要。服务器加载上载的生成文件。我将使用preg_替换为Inspire。无论如何谢谢你!这会起作用,但感觉不是解决问题的最佳方法。如果我没有找到更好的方法,我可能会使用这个。Regex很慢,这将允许您在线性时间内创建一个新文件,假设您在迭代每个文件时创建输出。regexp是否更慢?因为我只在本地主机上生成CSS文件,所以速度对我来说并不重要。服务器加载上载的生成文件。我将使用preg_替换为Inspire。无论如何谢谢你!这将删除@import,但我需要删除该行。它应该删除@import和\n之间的信息。这将删除@import,但我需要删除该行。它应该删除@import和\n之间的信息。如果您想删除您提到的行,“在我的本地安装中,我有一些@import行需要删除。”还将替换
@import url('something.css');body{color:#fff;}
和我希望的}
一样短。它像预期的那样工作。它删除带有@import的行。谢谢这是如果您想删除您提到的行:“在我的本地安装中,我有一些需要删除的@import行。”还将替换@import url('something.css');body{color:#fff;}
和我希望的}
一样短。它像预期的那样工作。它删除带有@import的行。谢谢你就不能用吗?你就不能用吗?我找到了一个由Inspire写的简短答案,只有一行。无论如何谢谢你!我找到了一个由Inspire写的简短答案,只有一行。无论如何谢谢你!