使用php分离rgba颜色值的红绿蓝值

时间:2014-03-13 17:08:52

标签: php

我以这种格式RGBA(205,31,31,1)的rgba值,我想分开每个红色,绿色,蓝色和alpha值以进一步处理如何使用php实现它;所以输出看起来像

red = 205
green = 31
blue = 31
alpha =1

2 个答案:

答案 0 :(得分:2)

preg_match与正则表达式一起使用:

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

答案 1 :(得分: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;