You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.3 KiB

<?php
define("MAXIMO_NUM_EXTENSO", 999999999.99);
function VocalizaData($numero, $rec = false, $cent = false) {
$valorI = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 600, 700, 800, 900);
/*
* A funcao trata numeros ate o valor da constante MAXIMO_NUM_EXTENSO.
*/
if ($numero > MAXIMO_NUM_EXTENSO) {
return false;
}
if ($rec) {
/*
* Armazena os numeros que serao retornados.
*/
$numeros = array();
/*
* Numero ja tratado.
*/
if (VocalizaDefinicao($valorI, $numero)) {
return array(VocalizaAdicao($numero, $numero));
}
$i = 0;
do {
$tam = strlen($numero);
$num = substr($numero, 0, 1);
/*
* Apenas numeros > 0 devem ser tratado aqui.
*/
if (!((int) $num)) {
break;
}
/*
* Adiciona um numero a ser vocalizado.
*/
$numeros[] = VocalizaAdicao($numero, ($num . str_repeat('0', $tam - 1)));
$num = (int) substr($numero, 1);
if (VocalizaDefinicao($valorI, $num)) {
$numeros[] = VocalizaAdicao($numero, $num);
break;
}
$i++;
$numero = $num;
} while ($tam > 1);
return $numeros;
}
$plur = array(1 => '', 2 => 'Mil');
//list($valor, $centavo) = explode('.', $numero);
$arVal = explode('.', $numero);
$numeros = array();
$i = 0;
foreach ($arVal as $valor) {
$cent = $i;
$valores = VocalizaReducao($valor); // print_r($valores); exit;
$idxExt = count($valores);
foreach ($valores as $vQuebra) {
foreach (VocalizaData((int) $vQuebra, true, $cent) as $vExt) {
$numeros[] = $vExt;
}
$ext = $vQuebra[0] > 1 ? $plur[$idxExt] : $sing[$idxExt];
if ($ext) {
$numeros[] = $ext;
}
$idxExt--;
}
$idxVl = $cent ? 1 : 0;
$ext = ((int) $valor > 1) ? $plur[0][$idxVl] : $sing[0][$idxVl];
if ($ext) {
$numeros[] = $ext;
}
$i++;
}
return VocalizaConexao($numeros);
}
function VocalizaReducao($valor) {
$tam = strlen($valor);
$arVl = array();
$str = '';
$cort = 0;
for ($i = 1; $i <= $tam; $i++) {
$str .= substr($valor, -$i, 1);
if (( ++$cort === 3) || ($i === $tam)) {
$arVl[] = strrev($str);
$str = '';
$cort = 0;
}
}
return array_reverse($arVl);
}
function VocalizaAdicao($numero, $numAdd) {
return ((int) $numAdd != 100) ? $numAdd : (((((int) $numAdd === 100) && $numero > 100) ? 'cento' : 'cem'));
}
function VocalizaDefinicao($valorI, $numero) {
return array_search($numero, $valorI) !== false;
}
function VocalizaConexao($numeros) {
$arExt = array('Mil');
$numRet = array();
for ($i = 0; $i < count($numeros); $i++) {
$numRet[] = $numeros[$i];
if (isset($numeros[$i + 1]) && (array_search($numeros[$i + 1], $arExt) === false)) {
$numRet[] = 'e';
}
}
return $numRet;
}