Php str_根据CSS注释替换CSS属性
我正在构建一个“主题生成器”,它将动态地编辑CSS文件。我认为使用PHP将是最容易的选择(愿意考虑替代方法)。 我的CSS文件在每个属性后都包含注释,如下所示:Php str_根据CSS注释替换CSS属性,php,javascript,css,ajax,str-replace,Php,Javascript,Css,Ajax,Str Replace,我正在构建一个“主题生成器”,它将动态地编辑CSS文件。我认为使用PHP将是最容易的选择(愿意考虑替代方法)。 我的CSS文件在每个属性后都包含注释,如下所示: html,body { background: #fff url(../images/bg.jpg) repeat-x; /*{bgColor}*/ color: #fff; /*{textColor}*/ } 是否可以使用替换函数来搜索注释并仅替换注释之前的代码?用户可能希望在完成主题构建后返回并再次更改某些内容
html,body {
background: #fff url(../images/bg.jpg) repeat-x; /*{bgColor}*/
color: #fff; /*{textColor}*/
}
是否可以使用替换函数来搜索注释并仅替换注释之前的代码?用户可能希望在完成主题构建后返回并再次更改某些内容,因此评论必须始终保留
谢谢您是在页面加载时生成CSS,还是在添加主题时重新生成CSS文件 如果你在编辑主题时生成CSS,你可以这样做
/*bodybg*/background:#fff url(../images/bg.jpg)repeat-x/*/bodybg*/
你可以这样做:
$shortCode = bodybg
$cssContents = preg_replace("/(\/\*".$shortCode."\*\/).*?(\/\*\/".$shortCode."\*\/)/i",
"\\1 background: #F00; \\2",
$cssContents);
如果在页面加载时生成CSS,可以执行以下操作:
background:{{bodyBgColor}}url(../images/bg.jpg)repeat-x代码>
$cssContents=str_replace({{bodyBgColor}}},$color,$cssContents)代码>您是在页面加载时生成CSS,还是在添加主题时重新生成CSS文件
如果你在编辑主题时生成CSS,你可以这样做
/*bodybg*/background:#fff url(../images/bg.jpg)repeat-x/*/bodybg*/
你可以这样做:
$shortCode = bodybg
$cssContents = preg_replace("/(\/\*".$shortCode."\*\/).*?(\/\*\/".$shortCode."\*\/)/i",
"\\1 background: #F00; \\2",
$cssContents);
如果在页面加载时生成CSS,可以执行以下操作:
background:{{bodyBgColor}}url(../images/bg.jpg)repeat-x代码>
$cssContents=str_replace({{bodyBgColor}}},$color,$cssContents)代码>在几年前做了类似的事情,但我是如何做到的,就是有一个会话变量,加载时将在CSS(或者更确切地说是PHP)文件中读取
所以。。。如果您创建一个php文件作为您的CSS文件并将其复制到其中
header("Content-type: text/css");
// setup replacement variables here...
// NOTE: if using the session object to start the session
// as the stylesheet is running in a seperate process as the rest of the site...
$textColor = "#ff0000"; // This is a variable that will appear in the CSS
$fHandle = @fopen("site.css", "r"); // Change this to your CSS file...
if ($fHandle) {
while (($line = fgets($fHandle, 4096)) !== false) {
$variable = getTextBetween($line,"/*{","}*/");
if ($variable != ""){
if (isSet($$variable)){
// we have that variable... now what to actually do with it...
// what we are going to do is rebuild the line...
$attribute = getTextBetween($line,0,":");
// and thats it really...
echo($attribute.":".$$variable.";".chr(10)); // NOTE: Double $$ to access the string as a variable :)
} else {
// that variable does not exist. Just output the line
echo $line;
}
} else {
// there is no variable just output the line
echo $line;
}
}
fclose($fHandle);
}
function getTextBetween($string_in,$start_in,$end_in){
$_start = 0;
$_end = 0;
// calculate the start and the end points.
if (is_string($start_in)){
$_start = strpos($string_in,$start_in);
if ($_start === false){
$_start = 0;
} else {
$_start += strlen($start_in);
}
} else if (is_numeric($start_in)){
$_start = $start_in;
}
if (is_string($end_in)){
$_end = strpos($string_in,$end_in,$_start);
if ($_end === false) $_end = 0;
} else if (is_numeric($end_in)){
$_end = $end_in;
}
$_return = substr($string_in,$_start,($_end-$_start));
return trim($_return);
}
然后以与普通样式表相同的方式包含该文件
如果您将所有变量设置为与示例中相同的名称。。。它将按照您希望的方式工作,而无需更改任何代码以适应其他工作方式:)
如果您需要任何帮助,请告诉我:)
祝你好运:)
爱所有人:)几年前做了一些非常类似的事情,但我是如何做的,它是有一个会话变量,加载时将在CSS(或者更确切地说是PHP)文件中读取
所以。。。如果您创建一个php文件作为您的CSS文件并将其复制到其中
header("Content-type: text/css");
// setup replacement variables here...
// NOTE: if using the session object to start the session
// as the stylesheet is running in a seperate process as the rest of the site...
$textColor = "#ff0000"; // This is a variable that will appear in the CSS
$fHandle = @fopen("site.css", "r"); // Change this to your CSS file...
if ($fHandle) {
while (($line = fgets($fHandle, 4096)) !== false) {
$variable = getTextBetween($line,"/*{","}*/");
if ($variable != ""){
if (isSet($$variable)){
// we have that variable... now what to actually do with it...
// what we are going to do is rebuild the line...
$attribute = getTextBetween($line,0,":");
// and thats it really...
echo($attribute.":".$$variable.";".chr(10)); // NOTE: Double $$ to access the string as a variable :)
} else {
// that variable does not exist. Just output the line
echo $line;
}
} else {
// there is no variable just output the line
echo $line;
}
}
fclose($fHandle);
}
function getTextBetween($string_in,$start_in,$end_in){
$_start = 0;
$_end = 0;
// calculate the start and the end points.
if (is_string($start_in)){
$_start = strpos($string_in,$start_in);
if ($_start === false){
$_start = 0;
} else {
$_start += strlen($start_in);
}
} else if (is_numeric($start_in)){
$_start = $start_in;
}
if (is_string($end_in)){
$_end = strpos($string_in,$end_in,$_start);
if ($_end === false) $_end = 0;
} else if (is_numeric($end_in)){
$_end = $end_in;
}
$_return = substr($string_in,$_start,($_end-$_start));
return trim($_return);
}
然后以与普通样式表相同的方式包含该文件
如果您将所有变量设置为与示例中相同的名称。。。它将按照您希望的方式工作,而无需更改任何代码以适应其他工作方式:)
如果您需要任何帮助,请告诉我:)
祝你好运:)
爱所有人:)只要您遵循这样一种模式,即在行尾有varname
,并且每行只包含一个CSS属性:value
,您就可以通过基于正则表达式的搜索和替换来完成
如果要执行此操作,请注意新值不包含PCRE意义上的任何换行符:\r\n | \n | \x0b |\f | \r | \x85
(非UTF-8模式)。如果您不这样做,这将破坏您的解析器
为此,您可以为该模式创建一个掩码,以便稍后可以轻松插入varname,我通常用于:
$patternMask =
'~
^ # start of line
(\s*[a-z]+:\s*)
# Group 1:
# whitespace (indentation)
# + CSS property and ":"
# + optional whitespace
(.*?) # Group 2: CSS value (to replace)
(\s*/\*\{%s\}\*/\s*)
# Group 3:
# whitespace (after value and before variable)
# + variable comment, %%s is placeholder for it\'s name
$ # end of line
# Pattern Modifiers:
# m: ^ & $ match begin/end of each line
# x: ignore spaces in pattern and allow comments (#)
~mx'
;
这是带有注释的正则表达式模式,可以使用x
-修饰符。只是为了让你更容易理解
重要的一点是多行模式的m
-修饰符。模式应适用于每一行,因此它包含在^
(开始)和$
(结束)中,这将在多行模式下匹配行的开始和结束
执行替换操作时,将替换组2,保留组1和3。这样,结果仍将包含变量名
然后,通过使用sprintf
将一个正确引用的变量名添加到实际的正则表达式模式中,并使用此掩码构建实际的正则表达式模式:
$patternMask[0]
是~
,因此如果变量名包含~
,它将自动正确转义
搜索模式现在已完成。剩下的是替代品。作为变量名,替换字符串也需要转义,以避免在正则表达式方面破坏它(语法错误)。此外,如前所述,整个流程需要注意将新字符串保留为一行,否则下次执行替换操作将使其中断。因此,为了防止出现这种情况,任何换行符都将替换为$value
中的单个空格,以防止出现这种情况:
# replace characters that will break the pattern with space
$valueFiltered = str_replace(explode('|', "\r\n|\n|\x0b|\f|\r|\x85"), ' ', $value);
然后,将引用特殊字符\
和$
,这样它们就不会干扰替换模式,并生成替换字符串。这是通过以下功能完成的:
# escape $ characters as they have a special meaning in the replace string
$valueEscaped = addcslashes($valueFiltered, '\$');
$replace = sprintf('${1}%s$3', $valueEscaped);
剩下的唯一一件事就是运行替换操作,因此预先为它提供ssome CSS:
$css = <<<CSS
html,body {
background: #fff url(../images/bg.jpg) repeat-x; /*{bgColor}*/
color: #fff; /*{textColor}*/
}
CSS;
这已经是整件事了。从最初的CSS:
html,body {
background: #fff url(../images/bg.jpg) repeat-x; /*{bgColor}*/
color: #fff; /*{textColor}*/
}
对于结果:
html,body {
background: #f00 url(../images/bg-reg.jpg) repeat-x; /*{bgColor}*/
color: #fff; /*{textColor}*/
}
如果使用preg\u replace
的和$count
参数,可以检查变量是否是字符串的一部分:
$newCss = preg_replace($pattern, $replace, $css, -1, $count);
$count
在给出的示例中为1
如果您想一次替换多个值,可以使用数组作为$pattern
和$replace
,以防有用$count
仍将是一个整数,因此它的用途可能有限
整个代码一目了然:
$css = <<<CSS
html,body {
background: #fff url(../images/bg.jpg) repeat-x; /*{bgColor}*/
color: #fff; /*{textColor}*/
}
CSS;
$patternMask =
'~
^ # start of line
(\s*[a-z]+:\s*)
# Group 1:
# whitespace (indentation)
# + CSS property and ":"
# + optional whitespace
(.*?) # Group 2: CSS value (to replace)
(\s*/\*\{%s\}\*/\s*)
# Group 3:
# whitespace (after value and before variable)
# + variable comment, %%s is placeholder for it\'s name
$ # end of line
# Pattern Modifiers:
# m: ^ & $ match begin/end of each line
# x: ignore spaces in pattern and allow comments (#)
~mx'
;
$varName = 'bgColor';
$value = '#f00 url(../images/bg-reg.jpg) repeat-x;';
# create regex pattern based on varname
$pattern = sprintf($patternMask, preg_quote($varName, $patternMask[0]));
# replace characters that will break the pattern with space
$valueFiltered = str_replace(explode('|', "\r\n|\n|\x0b|\f|\r|\x85"), ' ', $value);
# escape $ characters as they have a special meaning in the replace string
$valueEscaped = addcslashes($valueFiltered, '\$');
$replace = sprintf('${1}%s$3', $valueEscaped);
$newCss = preg_replace($pattern, $replace, $css);
echo $newCss;
$css=只要遵循这样的模式,即在行尾有varname
,并且每行只包含一个css属性:value
,就可以使用基于正则表达式的搜索和替换来完成
如果要执行此操作,请注意新值不为n