Checking Correctness of Hex Color Code with PHP

I needed to verify if the color value entered was hex or not. As a result of a little research, I verified with the following codes. I'm sharing it in case you need it one day.

$color = 'fa6582';
if (ctype_xdigit($color) && (strlen($color) == 6 || strlen($color) == 3)){
   echo 'valid hex color code';
} else {
   echo 'an invalid hex code';

good work


There are no comments, make the firs comment