-
-
Save alexjosesilva/89b2be1b2b2e7ba64d15dc5228dfcf97 to your computer and use it in GitHub Desktop.
Converte números e exibe por extenso em php Fonte: http://www.dirceuresende.com/blog/escrevendo-numero-por-extenso-no-php/
This file contains hidden or 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 | |
/** | |
* Created by PhpStorm. | |
* User: ALEX JOSE | |
* Date: 22/11/2017 | |
* Time: 14:30 | |
*/ | |
class Extenso | |
{ | |
public static function removerFormatacaoNumero( $strNumero ) | |
{ | |
$strNumero = trim( str_replace( "R$", null, $strNumero ) ); | |
$vetVirgula = explode( ",", $strNumero ); | |
if ( count( $vetVirgula ) == 1 ) | |
{ | |
$acentos = array("."); | |
$resultado = str_replace( $acentos, "", $strNumero ); | |
return $resultado; | |
} | |
else if ( count( $vetVirgula ) != 2 ) | |
{ | |
return $strNumero; | |
} | |
$strNumero = $vetVirgula[0]; | |
$strDecimal = mb_substr( $vetVirgula[1], 0, 2 ); | |
$acentos = array("."); | |
$resultado = str_replace( $acentos, "", $strNumero ); | |
$resultado = $resultado . "." . $strDecimal; | |
return $resultado; | |
} | |
public static function converte( $valor = 0, $bolExibirMoeda = true, $bolPalavraFeminina = false ) | |
{ | |
$valor = self::removerFormatacaoNumero( $valor ); | |
$singular = null; | |
$plural = null; | |
if ( $bolExibirMoeda ) | |
{ | |
$singular = array("centavo", "real", "mil", "milhão", "bilhão", "trilhão", "quatrilhão"); | |
$plural = array("centavos", "reais", "mil", "milhões", "bilhões", "trilhões","quatrilhões"); | |
} | |
else | |
{ | |
$singular = array("", "", "mil", "milhão", "bilhão", "trilhão", "quatrilhão"); | |
$plural = array("", "", "mil", "milhões", "bilhões", "trilhões","quatrilhões"); | |
} | |
$c = array("", "cem", "duzentos", "trezentos", "quatrocentos","quinhentos", "seiscentos", "setecentos", "oitocentos", "novecentos"); | |
$d = array("", "dez", "vinte", "trinta", "quarenta", "cinquenta","sessenta", "setenta", "oitenta", "noventa"); | |
$d10 = array("dez", "onze", "doze", "treze", "quatorze", "quinze","dezesseis", "dezesete", "dezoito", "dezenove"); | |
$u = array("", "um", "dois", "três", "quatro", "cinco", "seis","sete", "oito", "nove"); | |
if ( $bolPalavraFeminina ) | |
{ | |
if ($valor == 1) | |
$u = array("", "uma", "duas", "três", "quatro", "cinco", "seis","sete", "oito", "nove"); | |
else | |
$u = array("", "um", "duas", "três", "quatro", "cinco", "seis","sete", "oito", "nove"); | |
$c = array("", "cem", "duzentas", "trezentas", "quatrocentas","quinhentas", "seiscentas", "setecentas", "oitocentas", "novecentas"); | |
} | |
$z = 0; | |
$valor = number_format( $valor, 2, ".", "." ); | |
$inteiro = explode( ".", $valor ); | |
for ( $i = 0; $i < count( $inteiro ); $i++ ) | |
for ( $ii = mb_strlen( $inteiro[$i] ); $ii < 3; $ii++ ) | |
$inteiro[$i] = "0" . $inteiro[$i]; | |
// $fim identifica onde que deve se dar junção de centenas por "e" ou por "," ;) | |
$rt = null; | |
$fim = count( $inteiro ) - ($inteiro[count( $inteiro ) - 1] > 0 ? 1 : 2); | |
for ( $i = 0; $i < count( $inteiro ); $i++ ) | |
{ | |
$valor = $inteiro[$i]; | |
$rc = (($valor > 100) && ($valor < 200)) ? "cento" : $c[$valor[0]]; | |
$rd = ($valor[1] < 2) ? "" : $d[$valor[1]]; | |
$ru = ($valor > 0) ? (($valor[1] == 1) ? $d10[$valor[2]] : $u[$valor[2]]) : ""; | |
$r = $rc . (($rc && ($rd || $ru)) ? " e " : "") . $rd . (($rd && $ru) ? " e " : "") . $ru; | |
$t = count( $inteiro ) - 1 - $i; | |
$r .= $r ? " " . ($valor > 1 ? $plural[$t] : $singular[$t]) : ""; | |
if ( $valor == "000") | |
$z++; | |
elseif ( $z > 0 ) | |
$z--; | |
if ( ($t == 1) && ($z > 0) && ($inteiro[0] > 0) ) | |
$r .= ( ($z > 1) ? " de " : "") . $plural[$t]; | |
if ( $r ) | |
$rt = $rt . ((($i > 0) && ($i <= $fim) && ($inteiro[0] > 0) && ($z < 1)) ? ( ($i < $fim) ? ", " : " e ") : " ") . $r; | |
} | |
$rt = mb_substr( $rt, 1 ); | |
return($rt ? trim( $rt ) : "zero"); | |
} | |
} | |
echo Extenso::converte('12,50', true, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Classe para transcrever a forma numérica por extensão.