Posted By

STaRDoGG on 11/03/12


Tagged

regex hex validation color colors validate


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

o0110o


Validate HTML Color (Both HEX & Named)| Regex


 / Published in: PHP
 

This function will take a HTML color as a string, and run it through some validation.

If $named (optional): is set to either 1 or TRUE it'll check if it was a named color first, otherwise itll first check for a valid Hex color that include thes hash (#) sign, and then will check for a valid Hex color that doesn't include the hash and add it to the color.

NOTE: If it fails anywhere along the validation, such as an invalid Hex color, no error notice is returned. You may want to add this in yourself, or perhaps return a default color, such as black, if it's needed for your use.

  1. function validate_html_color($color, $named) {
  2. /* Validates hex color, adding #-sign if not found. Checks for a Color Name first to prevent error if a name was entered (optional).
  3.   * $color: the color hex value stirng to Validates
  4.   * $named: (optional), set to 1 or TRUE to first test if a Named color was passed instead of a Hex value
  5.   */
  6.  
  7. if ($named) {
  8.  
  9. $named = array('aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure', 'beige', 'bisque', 'black', 'blanchedalmond', 'blue', 'blueviolet', 'brown', 'burlywood', 'cadetblue', 'chartreuse', 'chocolate', 'coral', 'cornflowerblue', 'cornsilk', 'crimson', 'cyan', 'darkblue', 'darkcyan', 'darkgoldenrod', 'darkgray', 'darkgreen', 'darkkhaki', 'darkmagenta', 'darkolivegreen', 'darkorange', 'darkorchid', 'darkred', 'darksalmon', 'darkseagreen', 'darkslateblue', 'darkslategray', 'darkturquoise', 'darkviolet', 'deeppink', 'deepskyblue', 'dimgray', 'dodgerblue', 'firebrick', 'floralwhite', 'forestgreen', 'fuchsia', 'gainsboro', 'ghostwhite', 'gold', 'goldenrod', 'gray', 'green', 'greenyellow', 'honeydew', 'hotpink', 'indianred', 'indigo', 'ivory', 'khaki', 'lavender', 'lavenderblush', 'lawngreen', 'lemonchiffon', 'lightblue', 'lightcoral', 'lightcyan', 'lightgoldenrodyellow', 'lightgreen', 'lightgrey', 'lightpink', 'lightsalmon', 'lightseagreen', 'lightskyblue', 'lightslategray', 'lightsteelblue', 'lightyellow', 'lime', 'limegreen', 'linen', 'magenta', 'maroon', 'mediumaquamarine', 'mediumblue', 'mediumorchid', 'mediumpurple', 'mediumseagreen', 'mediumslateblue', 'mediumspringgreen', 'mediumturquoise', 'mediumvioletred', 'midnightblue', 'mintcream', 'mistyrose', 'moccasin', 'navajowhite', 'navy', 'oldlace', 'olive', 'olivedrab', 'orange', 'orangered', 'orchid', 'palegoldenrod', 'palegreen', 'paleturquoise', 'palevioletred', 'papayawhip', 'peachpuff', 'peru', 'pink', 'plum', 'powderblue', 'purple', 'red', 'rosybrown', 'royalblue', 'saddlebrown', 'salmon', 'sandybrown', 'seagreen', 'seashell', 'sienna', 'silver', 'skyblue', 'slateblue', 'slategray', 'snow', 'springgreen', 'steelblue', 'tan', 'teal', 'thistle', 'tomato', 'turquoise', 'violet', 'wheat', 'white', 'whitesmoke', 'yellow', 'yellowgreen');
  10.  
  11. if (in_array(strtolower($color), $named)) {
  12. /* A color name was entered instead of a Hex Value, so just exit function */
  13. return $color;
  14. }
  15. }
  16.  
  17. if (preg_match('/^#[a-f0-9]{6}$/i', $color)) {
  18. // Verified OK
  19. }else if (preg_match('/^[a-f0-9]{6}$/i', $color)) {
  20. $color = '#' . $color;
  21. }
  22. return $color;
  23. }

Report this snippet  

You need to login to post a comment.