I need convert a HSL or RGB string color to array. I get by parameter something like:
$str = "rgb(40, 50, 60)"; // or "hsl(40, 50%, 60%)"
convert to:
array(40, 50, 60); // or array(50, "50%", "60%")
I preferer a solution using regex, I dont want to use split function ;-(
NOTE: I have a solution for RGB, but I dont work with HSL:
$rgb_color = sscanf($str, "rgb(%d, %d, %d)");
You just need to look for numbers and %
symbol, \d
in regex find you any digits. And preg_match_all
php function return all of the matches not just the 1st one.
$matches = null;
$returnValue = preg_match_all('/([\d\%]+)/', 'hsl(40, 50%, 60%)', $matches);
var_dump($matches);
You can look for digits followed by an optional %
:
preg_match_all('/([\d]+)%?/', $str, $matches);
Then use $matches[1]
.