Convert HEX color to RGB values in PHP


What would be a good way to convert hex color values like #ffffff into the single RGB values 255 255 255 using PHP?


Example 1:

Check out PHP's hexdec() and dechex() functions.

$value = hexdec('ff'); // $value = 255

Example 2:

If you want to convert hex to rgb you can use sscanf.

$hex = "#ff9900";
list($r, $g, $b) = sscanf($hex, "#%02x%02x%02x");
echo "$hex -> $r $g $b"; // #ff9900 -> 255 153 0


Popular posts from this blog

About 12 Alwars

Post thumbnails from external URLs don’t appear in Blogger. How to resolve?