Anni
Anni

Reputation: 152

seperate Red Green Blue value of rgba Colur value using php

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

Upvotes: 0

Views: 126

Answers (2)

Cody Caughlan
Cody Caughlan

Reputation: 32748

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

Upvotes: 2

Mark Baker
Mark Baker

Reputation: 212402

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;

Upvotes: 1

Related Questions