0

I'hv rgba value in this format RGBA(205,31,31,1) and I want to separate each red, green, blue and alpha value for further processing how can I achieve it using php; so the output looks like

red = 205
green = 31
blue = 31
alpha =1
Anni
  • 142
  • 2
  • 16
  • Possible duplicate of [seperate Red Green Blue value of rgba Colur value](http://stackoverflow.com/questions/22369441/seperate-red-green-blue-value-of-rgba-colur-value) – Mark Baker Mar 13 '14 at 17:17

2 Answers2

2

Use preg_match with a regular expression:

$string = "RGBA(205,31,31,1)";
if(preg_match("/RGBA\((\d+),(\d+),(\d+),(\d+)\)/", $string, $matches)) {
  // $matches[0] contains the complete matched value, so we can ignore that
  echo "red = " . $matches[1] . "\n";
  echo "green = " . $matches[2] . "\n";
  echo "blue = " . $matches[3] . "\n";
  echo "alpha = " . $matches[4] . "\n";
}
Cody Caughlan
  • 32,456
  • 5
  • 63
  • 68
1
sscanf($myRGBString, 'RGBA(%d,%d,%d,%d)', $red, $green, $blue, $alpha);
echo 'red = ', $red, PHP_EOL;
echo 'green = ', $green, PHP_EOL;
echo 'blue = ', $blue, PHP_EOL;
echo 'alpha = ', $alpha, PHP_EOL;
Mark Baker
  • 209,507
  • 32
  • 346
  • 385