+ 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);
4 ответов
+ 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;
}
+ 1
Show your code here, it would be easier for you to get the solution.
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
0
My Code:
$rgbVal = $_POST['bannerClr'];
// what i want is : #fff00
$hexVal = sprintf("#%02x%02x%02x", $rgbVal);