View Single Post
Old 11-03-2007, 12:29 PM   #14 (permalink)
Salathe
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Jim View Post
Check if a colour is at least 6 chars after the # of course is fine, but people can also use a 3 char code (eg #000 also returns black), to really make it perfect you can also check if it's 3 chars before returning False.
If that's the case, shouldn't the code also allow the valid names for colours (red, blue, black, etc)? Since Wildhoney amended his colour function, here's mine -- just a tiny change to the RegExp. :)

PHP Code:
function is_colour($hex)
{
    
// True for hexadecimal strings of three or six characters in length
    // with an optional preceding hash.
    
return (bool) preg_match('/^#?[a-fA-F0-9]{3}|[a-fA-F0-9]{6}$/'$hex);

Salathe is offline  
Reply With Quote