How to get hex code from rgba using php ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to get hex code from rgba using php ?

I have rgba post values , but I wanna save it as hex.. someone have solution help me out. MY CODESET: $rgbVal = $_POST['bannerClr']; // what i want is : #fff00 $hexVal = sprintf("#%02x%02x%02x", $rgbVal);

21st Aug 2017, 9:11 AM
Chandrasekaran P
Chandrasekaran P - avatar
4 Answers
+ 1
Here is the working code for the issue: // $c - rgba or Hex code public function rgb2hex2rgb($c){ if(!$c) return false; $c = trim($c); $out = false; if(preg_match("/^[0-9ABCDEFabcdef\#]+$/i", $c)){ $out = $c; }elseif (preg_match("/^[0-9]+(,| |.)+[0-9]+(,| |.)+[0-9]+$/i", $c)){ $spr = str_replace(array(',',' ','.'), ':', $c); $e = explode(":", $spr); if(count($e) != 3) return false; $out = '#'; for($i = 0; $i<3; $i++) $e[$i] = dechex(($e[$i] <= 0)?0:(($e[$i] >= 255)?255:$e[$i])); for($i = 0; $i<3; $i++) $out .= ((strlen($e[$i]) < 2)?'0':'').$e[$i]; $out = strtoupper($out); } else $out = '0'; return $out; }
11th Sep 2017, 10:08 AM
Chandrasekaran P
Chandrasekaran P - avatar
+ 1
Show your code here, it would be easier for you to get the solution.
21st Aug 2017, 9:06 AM
Calviղ
Calviղ - avatar
0
have you tried to convert from binary to hex? I haven't learned php but binary to hex is litterally 00000001 to 0001 or 00000010 to 0010
21st Aug 2017, 9:09 AM
Tristen Alexander Diaz
Tristen Alexander Diaz - avatar
0
My Code: $rgbVal = $_POST['bannerClr']; // what i want is : #fff00 $hexVal = sprintf("#%02x%02x%02x", $rgbVal);
21st Aug 2017, 1:03 PM
Chandrasekaran P
Chandrasekaran P - avatar