Last active
February 16, 2020 15:15
-
-
Save Erenor/260209032cedc9eb3bf583c00f27d878 to your computer and use it in GitHub Desktop.
[PHP] A simple function to get a 6-chars hexadecimal color starting from any string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Function takes a random string and returns an hex color in the 6-digits format | |
* | |
* @param string $input | |
* | |
* @return string | |
*/ | |
function meow_str_to_hex_color($input) { | |
//characters that we do not want into an hex color | |
$replace = range('g', 'z'); | |
//md5 of the string (random chars, but always the same for the same start string) | |
$str = md5($input); | |
//remove unwanted chars | |
$str = str_replace($replace, '', $str); | |
//check lenght | |
if (strlen($str) < 6) { | |
//uhhh too short! make it 6-chars long with some zeros | |
$str = str_pad ($str , 6, 0, STR_PAD_LEFT); | |
} | |
else { | |
//oh, too long: use first 6 chars | |
$str = substr($str, 0, 6); | |
} | |
return $str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment