Convert hex color to RGB values in PHP

user123_456 picture user123_456 · Mar 4, 2013 · Viewed 85.4k times · Source

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

Answer

John picture John · Mar 4, 2013

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

<?php
$hex = "#ff9900";
list($r, $g, $b) = sscanf($hex, "#%02x%02x%02x");
echo "$hex -> $r $g $b";
?>

Output:

#ff9900 -> 255 153 0