I'm writing a function that replaces long hex coded color (#334455
) with short one (#345
). This can be only done when each color in hex is multiple of 17 (each hex pair consists of the same characters).
e.g. #EEFFCC
is replaced with #EFC
, but #EDFFCC
isn't replaced with anything.
I want to make this with single preg_replace()
call without any custom callbacks.
I've already tried this:
$hex = preg_replace('/([0-f]){2}([0-f]){2}([0-f]){2}/i', '\1\2\3', $hex);
But that shortens all hexes, not just the hexes with same characters in each pair. I can't figure out how to match only pairs of same character.
Please help.