下面的函数接受输入文件流并将 $color 值替换为数据库中存储的值(color1=ffffff、color2=aaaaaa、color3=cccccc 等)
目前它只对 6 位十六进制值进行直接查找/替换。是否可以将正则表达式添加到 my_colorReplace 函数中,以便根据输入流模式返回 6 位十六进制或 9 位 rgb?
function my_colorReplace($buffer) {
/* NEED REGEX HERE TO DETERMINE WHETHER TO CALL CONVERTHEXTORGB() */
$buffer = str_replace(array('$color1'), '#'.get_option("my_theme_header_color").'', $buffer);
$buffer = str_replace(array('$color2'), '#'.get_option("my_theme_sidebar_color").'', $buffer);
$buffer = str_replace(array('$color3'), '#'.get_option("my_theme_spot_color_alt").'', $buffer);
$buffer = str_replace(array('$color4'), '#'.get_option("my_theme_spot_color_alt2").'', $buffer);
return $buffer;
}
将十六进制值转换为 rgb 的实用函数
function convertHexToRGB($hexColor){
if( preg_match( '/^#?([a-h0-9]{2})([a-h0-9]{2})([a-h0-9]{2})$/i', $hexColor, $matches ) )
{
return array('red' => hexdec( $matches[1] ),'green' => hexdec( $matches[2] ),'blue' => hexdec( $matches[3] ));
}
else
{
return array( 120, 120, 120 );
}
}
输入流($buffer):
.sidebar{
background:$color1;
color:$color2;
}
.header{
background:
linear-gradient(to bottom, rgba($color1,.5) 0%,
rgba(255,255,255,0.96) 100px,
rgba(255,255,255,0.95) 150px); /* W3C */
color:rgb($color3);
}
$buffer return 的预期输出(其中 color1=ffffff,color2=aaaaaa,color3=cccccc
.sidebar{
background:#fffff;
color:#aaaaaa;
}
.header{
background:
linear-gradient(to bottom, rgba(255,255,255,.5) 0%,
rgba(255,255,255,0.96) 100px,
rgba(255,255,255,0.95) 150px); /* W3C */
color:rgb(204,204,204);
}