Convert hex color to RGB values in PHP

前端 未结 15 1858
轮回少年
轮回少年 2020-11-30 21:32

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

15条回答
  •  执念已碎
    2020-11-30 22:30

    You can use the function hexdec(hexStr: String) to get the decimal value of a hexadecimal string.

    See below for an example:

    $split = str_split("ffffff", 2);
    $r = hexdec($split[0]);
    $g = hexdec($split[1]);
    $b = hexdec($split[2]);
    echo "rgb(" . $r . ", " . $g . ", " . $b . ")";
    

    This will print rgb(255, 255, 255)

提交回复
热议问题