Last active
August 28, 2019 18:24
-
-
Save Flower7C3/14a115f42e03280aa8f9e6fe5ba10b9b to your computer and use it in GitHub Desktop.
Pronounce HEX value
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
<?php | |
class HexTools | |
{ | |
private static $verbalMap = [ | |
'00' => 'zero', | |
'-0' => '', | |
'0-' => '', | |
'-1' => 'one', | |
'1-' => 'teen', | |
'11' => 'eleven', | |
'12' => 'twelve', | |
'13' => 'thirteen', | |
'15' => 'fifteen', | |
'-2' => 'two', | |
'2-' => 'twenty', | |
'-3' => 'three', | |
'3-' => 'thirty', | |
'-4' => 'four', | |
'4-' => 'fourty', | |
'-5' => 'five', | |
'5-' => 'fifty', | |
'-6' => 'six', | |
'6-' => 'sixty', | |
'-7' => 'seven', | |
'7-' => 'seventy', | |
'-8' => 'eight', | |
'8-' => 'eighty', | |
'-9' => 'nine', | |
'9-' => 'ninety', | |
'-A' => 'ann', | |
'A-' => 'annty', | |
'-B' => 'bet', | |
'B-' => 'betty', | |
'-C' => 'chris', | |
'C-' => 'christy', | |
'-D' => 'dot', | |
'D-' => 'dotty', | |
'-E' => 'ernest', | |
'E-' => 'ernestty', | |
'-F' => 'frost', | |
'F-' => 'frostty', | |
]; | |
public static function hex2verbal($hex) | |
{ | |
$verbal = ''; | |
$hex = strtoupper($hex); | |
$hex = preg_replace('([^0-9A-F])', '', $hex); | |
$hexLen = strlen($hex); | |
if ($hexLen % 2 === 1) { | |
$hex = str_pad($hex, $hexLen + 1, '0', STR_PAD_LEFT); | |
} | |
$parts = str_split($hex, 2); | |
foreach ($parts as $part) { | |
$part = sprintf('%02s', $part); | |
$word = self::$verbalMap[$part]; | |
if (!empty($word)) { | |
$verbal .= $word; | |
} else { | |
$part1 = (string)$part[0] . '-'; | |
$part2 = (string)'-' . $part[1]; | |
if ($part1 === '1-') { | |
$word1 = self::$verbalMap[$part2]; | |
$word2 = self::$verbalMap[$part1]; | |
$divider = ''; | |
} else { | |
$word1 = self::$verbalMap[$part1]; | |
$word2 = self::$verbalMap[$part2]; | |
$divider = '-'; | |
} | |
$verbal .= $word1; | |
if (!empty($word1) && !empty($word2)) { | |
$verbal .= $divider; | |
} | |
$verbal .= $word2; | |
} | |
$verbal .= ' '; | |
} | |
return trim($verbal); | |
} | |
} | |
$hex = empty($argv[1]) ? '0' : $argv[1]; | |
echo $hex .' = ' .HexTools::hex2verbal($hex) . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment