Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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);
Add Comment
Please, Sign In to add comment