9951 explained code solutions for 126 technologies


php-regexHow to use PHP regex to match a hex color?


To match a hex color with PHP regex, you can use the following code:

$hex_color = "#FFF";
if (preg_match('/^#[a-f0-9]{6}$/i', $hex_color)) {
    echo "Valid hex color";
} else {
    echo "Invalid hex color";
}

The output of this code will be:

Valid hex color

The code consists of the following parts:

  • $hex_color = "#FFF"; - this is the variable containing the hex color to be matched.
  • preg_match('/^#[a-f0-9]{6}$/i', $hex_color) - this is the regular expression used to match the hex color. It matches a string that starts with a # followed by 6 characters from a-f or 0-9 and ends with the same character. The i flag at the end makes the expression case-insensitive.
  • echo "Valid hex color"; - this is the output when the hex color is valid.
  • echo "Invalid hex color"; - this is the output when the hex color is invalid.

Helpful links

Edit this code on GitHub