Created
October 10, 2019 23:57
-
-
Save dptole/480bc690f4139c7813e60d7b276d5998 to your computer and use it in GitHub Desktop.
Converter números para texto em PHP
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 | |
// UDC = Unidade Dezena Centena | |
function numeroParaTextoUDC($numero) { | |
$_ucd = "$numero"; | |
while(strlen($_ucd) < 3) | |
$_ucd = "0$_ucd"; | |
switch(+$_ucd) { | |
case 0: return array("zero"); | |
case 1: return array("um"); | |
case 2: return array("dois"); | |
case 3: return array("três"); | |
case 4: return array("quatro"); | |
case 5: return array("cinco"); | |
case 6: return array("seis"); | |
case 7: return array("sete"); | |
case 8: return array("oito"); | |
case 9: return array("nove"); | |
case 10: return array("dez"); | |
case 11: return array("onze"); | |
case 12: return array("doze"); | |
case 13: return array("treze"); | |
case 14: return array("quatorze", "catorze"); | |
case 15: return array("quinze"); | |
case 16: return array("dezesseis"); | |
case 17: return array("dezessete"); | |
case 18: return array("dezoito"); | |
case 19: return array("dezenove"); | |
case 20: return array("vinte"); | |
case 30: return array("trinta"); | |
case 40: return array("quarenta"); | |
case 50: return array("cinquenta"); | |
case 60: return array("sessenta"); | |
case 70: return array("setenta"); | |
case 80: return array("oitenta"); | |
case 90: return array("noventa"); | |
case 100: return array("cem", "cento"); | |
case 200: return array("duzentos"); | |
case 300: return array("trezentos"); | |
case 400: return array("quatrocentos"); | |
case 500: return array("quinhentos"); | |
case 600: return array("seiscentos"); | |
case 700: return array("setecentos"); | |
case 800: return array("oitocentos"); | |
case 900: return array("novecentos"); | |
default: | |
if($_ucd[0] === "0") { | |
// dezenas - 0xx | |
$_d = numeroParaTextoUDC($_ucd[1] . "0"); | |
$_u = $_ucd[2] !== "0" ? numeroParaTextoUDC($_ucd[2]) : ""; | |
return array($_d[0] . (!empty($_u) ? " e " . $_u[0] : | |
$_u)); | |
} else { | |
// centenas - xxx | |
if($_ucd[0] === "1" && substr($_ucd, 1) == "00") | |
return numeroParaTextoUDC($_ucd[0] . "00"); | |
$_c = numeroParaTextoUDC($_ucd[0] . "00"); | |
$_d = ""; | |
$_u = ""; | |
if($_ucd[1] !== "1" && $_ucd[2] !== "0") { | |
$tmp = numeroParaTextoUDC($_ucd[2]); | |
$_u = array_pop($tmp); | |
} if($_ucd[1] !== "0") | |
$tmp = numeroParaTextoUDC($_ucd[1] . ($_ucd[1] === "1" ? $_ucd[2] : "0")); | |
$_d = array_pop($tmp); | |
return array( | |
(count($_c) > 1 ? | |
(empty($_ucd[1]) && empty($_ucd[2]) ? | |
$_c[0] | |
: | |
$_c[1]) | |
: | |
$_c[0]) . | |
(!empty($_d) ? | |
" e $_d" | |
: | |
"") . | |
(!empty($_u) ? | |
" e $_u" | |
: | |
"") | |
); | |
} | |
break; | |
} | |
return ""; | |
} | |
function numeroParaTextoPrefixo($numero) { | |
$_l = strlen("$numero"); | |
if($_l >= 4) | |
if($_l <= 6) { | |
return array("mil", "mil"); | |
} elseif($_l <= 9) { | |
return array("milhão", "milhões"); | |
} elseif($_l <= 12) { | |
return array("bilhão", "bilhões"); | |
} elseif($_l <= 15) { | |
return array("trilhão", "trilhões"); | |
} elseif($_l <= 18) { | |
return array("quadrilhão", "quadrilhões"); | |
} elseif($_l <= 21) { | |
return array("quintilhão", "quintilhões"); | |
} elseif($_l <= 24) { | |
return array("sextilhão", "sextilhões"); | |
} elseif($_l <= 27) { | |
return array("setilhão", "setilhões"); | |
} elseif($_l <= 30) { | |
return array("octilhão", "octilhões"); | |
} elseif($_l <= 33) { | |
return array("nonilhão", "nonilhões"); | |
} elseif($_l <= 36) { | |
return array("decilhão", "decilhões"); | |
} | |
return array("", ""); | |
}; | |
function numeroParaTexto($n) { | |
if((is_numeric($n) && is_string($n)) || is_integer($n) || is_float($n)) { | |
$n = $_old_n = "$n"; | |
$_l = 0; | |
$_deslocar = 3; | |
$_numero_por_extenso = array(); | |
while(strlen($n) % $_deslocar) | |
$n = "0$n"; | |
while($_l < strlen($n)) { | |
$_n = substr($n, $_l); | |
$_ucd = +(substr($_n, 0, $_deslocar)); | |
if(!$_ucd) { | |
$_l += $_deslocar; | |
continue; | |
} | |
$_ns = "" . (+$_n); | |
$_m = strlen($_ns) > 3 && strlen($_ns) < 7; | |
$_p = numeroParaTextoPrefixo($_n); | |
$_p = $_ucd === 1 ? $_p[0] : $_p[1]; | |
if(strlen($_ns) < 4) { | |
$tmp = numeroParaTextoUDC($_ucd); | |
$_numero_por_extenso[] = array_shift($tmp); | |
} else if(!($_m && +$_ns === 1)) { | |
$_num = numeroParaTextoUDC($_ucd); | |
if(!($_ucd == "1" && $_m)) | |
$_numero_por_extenso[] = $_num[0]; | |
} | |
$_numero_por_extenso[] = $_p; | |
if($_m && substr($n, $_l + $_deslocar, 1) === "0" && substr($n, $_l + $_deslocar) != "000") | |
$_numero_por_extenso[] = "e"; | |
$_l += $_deslocar; | |
} | |
return implode(" ", $_numero_por_extenso); | |
} | |
return false; | |
} | |
$r = rand(); | |
header('content-type: text/plain'); | |
die($r . "\n" . numeroParaTexto($r)); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment