I do not know if they can help me but I want to replace the color in "#xxxxxx" format in a CSS through Regular Expression and PHP.
I have a function that calculates the color and replaces it with the inverse color. I need to get the CSS and invert all colors in the "#xxxxxx" format by their negative color. I do not know how best to do this.
I have the function that reverses the colors ready.
FUNCTION inverseHex( $color )
$color = TRIM($color);
$prependHash = FALSE;
IF(STRPOS($color,'#')!==FALSE) {
$prependHash = TRUE;
$color = STR_REPLACE('#',NULL,$color);
}
SWITCH($len=STRLEN($color)) {
CASE 3:
$color=PREG_REPLACE("/(.)(.)(.)/","\1\1\2\2\3\3",$color);
CASE 6:
BREAK;
DEFAULT:
TRIGGER_ERROR("Invalid hex length ($len). Must be (3) or (6)", E_USER_ERROR);
}
IF(!PREG_MATCH('/[a-f0-9]{6}/i',$color)) {
$color = HTMLENTITIES($color);
TRIGGER_ERROR( "Invalid hex string #$color", E_USER_ERROR );
}
$r = DECHEX(255-HEXDEC(SUBSTR($color,0,2)));
$r = (STRLEN($r)>1)?$r:'0'.$r;
$g = DECHEX(255-HEXDEC(SUBSTR($color,2,2)));
$g = (STRLEN($g)>1)?$g:'0'.$g;
$b = DECHEX(255-HEXDEC(SUBSTR($color,4,2)));
$b = (STRLEN($b)>1)?$b:'0'.$b;
RETURN ($prependHash?'#':NULL).$r.$g.$b;
and I have the regular expressions to find the colors:
#\b\w{5}+[d]\b
But I do not know how to use preg_replace to scroll through the text and replace each instance, not with another array but passing the inverseHex () function above, and do this throughout the CSS. to print CSS with all negative colors
If someone gives me a tip, I can already turn around to produce the code. I have to do this in 30 CSS files with thousands of lines each.