Regex replacement with hex to rgba () conversion

The function below takes the input stream and replaces the color values ​​$ with the saved values ​​from the database (color1 = ffffff, color2 = aaaaaa, color3 = cccccc, etc.)

It currently performs direct detection / replacement of six-digit six-digit values. Is there a regex that I can add to my_colorReplace function to return a six-digit hexadecimal or 9-digit rgb based on the input stream pattern?

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; } 

Utility to convert hex value to 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 ); } } 

Input stream ($ 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); } 

The expected return result is $ buffer (where 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); } 
+4
source share

Source: https://habr.com/ru/post/1468837/


All Articles