正则表达式替换为十六进制到rgba()转换

时间:2013-03-12 21:26:03

标签: php regex

下面的函数采用输入文件流,并将$ color值替换为数据库中存储的值(color1 = ffffff,color2 = aaaaaa,color3 = cccccc等)

目前它只对6位十六进制值进行直接查找/替换。有没有我可以添加到my_colorReplace functon的正则表达式,以便根据输入流模式返回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返回的预期输出(其中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); 
}

0 个答案:

没有答案