Verify hex color string

Verify hex color string

This php script checks is the string $color is a hex color or not. The second IF statement will add the pound symbol at the beginning if it is missing from the hex color value.

<?php

//Check for a hex color string '#c1c2b4'
if(preg_match('/^#&#91;a-f0-9&#93;{6}$/i', $color)) //hex color is valid
{
      //Verified hex color
} 

//Check for a hex color string without hash 'c1c2b4'
else if(preg_match('/^&#91;a-f0-9&#93;{6}$/i', $color)) //hex color is valid
{
      $fix_color = '#' . $color;
} 

?>

Source:
http://code.hyperspatial.com/250/verify-hex-color-string/

Pin It on Pinterest

Share This