Blog
Wild & Free Tools

How to Convert Hex to RGB in PHP

Last updated: February 2026 4 min read
Quick Answer

Table of Contents

  1. Basic Hex to RGB Function
  2. sscanf Alternative
  3. Returning CSS String Output
  4. Handling Three-Character Hex
  5. Frequently Asked Questions

PHP makes hex-to-RGB conversion straightforward with two built-in functions: substr() to extract each two-character color channel, and hexdec() to convert from hexadecimal to a decimal integer. The result is three integers between 0 and 255 that you can use for CSS output, image processing, or data storage.

A Basic PHP Hex to RGB Function

Clean and production-ready:

function hexToRgb(string $hex): array {
    $hex = ltrim($hex, '#');
    return [
        'r' => hexdec(substr($hex, 0, 2)),
        'g' => hexdec(substr($hex, 2, 2)),
        'b' => hexdec(substr($hex, 4, 2)),
    ];
}

$rgb = hexToRgb('#3B82F6');
// ['r' => 59, 'g' => 130, 'b' => 246]

echo "rgb({$rgb['r']}, {$rgb['g']}, {$rgb['b']})";
// rgb(59, 130, 246)

ltrim($hex, '#') removes the hash prefix if present. hexdec() converts any hex string (case-insensitive) to a decimal integer. substr() extracts the two-character slice for each channel.

Using sscanf for a One-Line Approach

PHP's sscanf() function can parse a hex string into three integers in a single call:

$hex = ltrim('#3B82F6', '#');
[$r, $g, $b] = sscanf($hex, '%02x%02x%02x');
// $r=59, $g=130, $b=246

The format string '%02x%02x%02x' reads three two-digit hex values in sequence. This is slightly more compact than the substr/hexdec approach and equally reliable. Both are valid choices.

Sell Custom Apparel — We Handle Printing & Free Shipping

Returning a CSS rgb() String Directly

If the goal is to generate inline styles or CSS output, return the formatted string directly:

function hexToCss(string $hex): string {
    $hex = ltrim($hex, '#');
    [$r, $g, $b] = sscanf($hex, '%02x%02x%02x');
    return "rgb($r, $g, $b)";
}

echo hexToCss('#FF6400');
// rgb(255, 100, 0)

This is useful when generating HTML templates server-side or when applying dynamic colors to image overlays with the GD library.

Handling Three-Character Hex Shorthand in PHP

Hex shorthand like #F60 needs expansion before parsing:

function normalizeHex(string $hex): string {
    $hex = ltrim($hex, '#');
    if (strlen($hex) === 3) {
        $hex = $hex[0].$hex[0].$hex[1].$hex[1].$hex[2].$hex[2];
    }
    return $hex;
}

// Usage:
$hex = normalizeHex('#F60');  // 'FF6600'
[$r, $g, $b] = sscanf($hex, '%02x%02x%02x');

Run normalization before any parsing call to handle both three-character and six-character input reliably.

Need a Quick Conversion?

Skip the PHP — paste your hex code above and copy the RGB values in one click.

Open Hex to RGB Converter

Frequently Asked Questions

What does hexdec() do in PHP?

hexdec() converts a hexadecimal string to its decimal (base 10) integer equivalent. hexdec("FF") returns 255, hexdec("3B") returns 59. It is case-insensitive and works on any length hex string.

Is there a built-in PHP function for hex to RGB?

No single built-in function does the full conversion. The combination of ltrim() + substr() + hexdec(), or ltrim() + sscanf(), is the standard pattern used across PHP projects.

Does this work in all PHP versions?

Both hexdec() and sscanf() have been in PHP since version 4. The typed parameter (string $hex) requires PHP 7.0+. For older PHP, remove the type hint: function hexToRgb($hex).

How do I convert RGB back to hex in PHP?

Use sprintf with the %02x format: sprintf("#%02x%02x%02x", $r, $g, $b). The %02x specifier converts an integer to a two-character lowercase hex string, zero-padded if needed.

Carlos Mendez
Carlos Mendez Photo Editing & Image Writer

Carlos has been a freelance photographer and photo editor for a decade, working with clients from local businesses to regional magazines.

More articles by Carlos →
Launch Your Own Clothing Brand — No Inventory, No Risk