0

I have a RGBA color in this format:

RGBA:1.000000,0.003922,0.003922,0.003922

How can I separate each value from this string such as:

var alpha = 1.000000;
var red = 0.003922;
var green = 0.003922;
var blue = 0.003922;

I want to do this in php.

4
  • What's your approach? Commented Apr 10, 2014 at 19:01
  • You can use split(). php.net/manual/en/function.split.php Commented Apr 10, 2014 at 19:02
  • 1
    Try PHP Explode function Commented Apr 10, 2014 at 19:02
  • @sanketh: Don't use split(). It's deprecated. Use explode() instead. Commented Apr 10, 2014 at 19:03

3 Answers 3

2

Split the string into two parts with : as the delimiter, take the second part, and split the result again with , as the delimiter. Assign the values to variables. All this could be done in one line, as follows:

list($alpha, $red, $green, $blue) = explode(',', explode(':', $str, 2)[1]);

Output:

string(8) "1.000000"
string(8) "0.003922"
string(8) "0.003922"
string(8) "0.003922

Demo

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

Comments

0

Code snippet should be like this:

$rgba = "1.000000,0.003922,0.003922,0.003922";
$rgba = explode(',', $rgba);
list($red, $green, $blue, $alpha) = $rgba;

Comments

0

Another way using regex is

$str = 'RGBA:1.000000,0.003922,0.003922,0.003922';
preg_match_all("/(\d+\.+\d+)/", $str, $output_array);

list($alpha, $red, $green, $blue) = $output_array[1] ;

https://eval.in/135090

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.