0

Is there any build-in PHP function to get the following result?

    $rgba = "rgba(246, 24, 35, 0.8)";
    $splittedRgba = splitRGB($rgba);

    $rgb = "rgb(246, 24, 35)";
    $splittedRgb = splitRGB($rgb); // splitRGB is the function I need
    /*
        $splittedRgba['r'] = 246;
        $splittedRgba['g'] = 24;
        $splittedRgba['b'] = 35;
        $splittedRgba['a'] = 0.8;

        $splittedRgb['r'] = 246;
        $splittedRgb['g'] = 24;
        $splittedRgb['b'] = 35;
        $splittedRgb['a'] = 1;
    */
3
  • 3
    There isn't from an arbitrary string, but preg_match() will do the job with an appropriate regexp Commented Sep 9, 2014 at 14:04
  • 2
    PHP is a toolbox. it contains screwdrivers, hammers, maybe a measuring tape and a pencil. You're expecting it to contain a fully developed house, complete with plumbing and electrical wiring for EVERY possible thing you want it to do. Instead of flailing around looking for a can opener that will cook your thanksgiving dinner and help your kids get into college, you should learn how to use the basic tools PHP does provide to BUILD that all-in-one tool. Commented Sep 9, 2014 at 14:18
  • 1
    @MarcB - don't forget the double claw Commented Sep 9, 2014 at 14:45

2 Answers 2

9

You can use a preg_match, but you can also use the simpler sscanf function:

$rgba = sscanf("rgba(246, 24, 35, 0.8)", "rgba(%d, %d, %d, %f)");

print_r($rgba);

$rgb = sscanf("rgb(246, 24, 35)", "rgb(%d, %d, %d)");

print_r($rgba);

%d matches a decimal number, %f matches a floating point number.

References:

http://php.net/list

http://php.net/sscanf

Don't expect people to provide you with fully working super functional code. Answers here make clear mistakes you made and should give you an idea helping to solve a more complex problem.

So your part here would be something like:

function parseRGBa($rgba) {
    $rgba = trim(str_replace(' ', '', $rgba));
    if (stripos($rgba, 'rgba') !== false) {
        $res = sscanf($rgba, "rgba(%d, %d, %d, %f)");
    }
    else {
        $res = sscanf($rgba, "rgb(%d, %d, %d)");
        $res[] = 1;
    }
    return array_combine(array('r', 'g', 'b', 'a'), $res);
}

print_r(parseRGBa('rgba(1, 2, 3, 0.5)'));

Demo.

Sign up to request clarification or add additional context in comments.

4 Comments

Not working with $rgb = "rgb(246, 24, 35)";. I don't know if the string starts with rgb or rgba.
Sorry, I did expect your transfer knowledge to be able to handle the missing code with your own imagination.
@DanFromGermany Very nice approach, just made a couple of adjustments in your code. )
The array code style [...] doesn't work in PHP prior to version 5.4, but of course you are welcome to extend my answer, good idea there!
1

Another solution using preg_match_all() function

$rgba = array("r" => 0, "g" => 0, "b" => 0, "a" => 1);
$string = "246, 24, 35, 0.8"; // or "rgba(246, 24, 35, 0.8)"
$i = 0;

preg_match_all('/[0-9.]+/', $string, $color);   
foreach ($rgba as $key => $value) {
  $rgba[$key] = $color[0][$i++] ?: 1;
}

print_r($rgba);

Output:

Array
(
    [r] => 246
    [g] => 24
    [b] => 35
    [a] => 0.8
)

In case you have this format

$string = "246, 24, 35"; // or "rgb(246, 24, 35)"

as expected, the output will be

Array
(
    [r] => 246
    [g] => 24
    [b] => 35
    [a] => 1
)

Working PHP demo

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.