Guest User

Untitled

a guest
Nov 24th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: ALEX JOSE
  5. * Date: 22/11/2017
  6. * Time: 14:30
  7. */
  8. class Extenso
  9. {
  10. public static function removerFormatacaoNumero( $strNumero )
  11. {
  12.  
  13. $strNumero = trim( str_replace( "R$", null, $strNumero ) );
  14.  
  15. $vetVirgula = explode( ",", $strNumero );
  16. if ( count( $vetVirgula ) == 1 )
  17. {
  18. $acentos = array(".");
  19. $resultado = str_replace( $acentos, "", $strNumero );
  20. return $resultado;
  21. }
  22. else if ( count( $vetVirgula ) != 2 )
  23. {
  24. return $strNumero;
  25. }
  26.  
  27. $strNumero = $vetVirgula[0];
  28. $strDecimal = mb_substr( $vetVirgula[1], 0, 2 );
  29.  
  30. $acentos = array(".");
  31. $resultado = str_replace( $acentos, "", $strNumero );
  32. $resultado = $resultado . "." . $strDecimal;
  33.  
  34. return $resultado;
  35.  
  36. }
  37.  
  38. public static function converte( $valor = 0, $bolExibirMoeda = true, $bolPalavraFeminina = false )
  39. {
  40.  
  41. $valor = self::removerFormatacaoNumero( $valor );
  42.  
  43. $singular = null;
  44. $plural = null;
  45.  
  46. if ( $bolExibirMoeda )
  47. {
  48. $singular = array("centavo", "real", "mil", "milhão", "bilhão", "trilhão", "quatrilhão");
  49. $plural = array("centavos", "reais", "mil", "milhões", "bilhões", "trilhões","quatrilhões");
  50. }
  51. else
  52. {
  53. $singular = array("", "", "mil", "milhão", "bilhão", "trilhão", "quatrilhão");
  54. $plural = array("", "", "mil", "milhões", "bilhões", "trilhões","quatrilhões");
  55. }
  56.  
  57. $c = array("", "cem", "duzentos", "trezentos", "quatrocentos","quinhentos", "seiscentos", "setecentos", "oitocentos", "novecentos");
  58. $d = array("", "dez", "vinte", "trinta", "quarenta", "cinquenta","sessenta", "setenta", "oitenta", "noventa");
  59. $d10 = array("dez", "onze", "doze", "treze", "quatorze", "quinze","dezesseis", "dezesete", "dezoito", "dezenove");
  60. $u = array("", "um", "dois", "três", "quatro", "cinco", "seis","sete", "oito", "nove");
  61.  
  62. if ( $bolPalavraFeminina )
  63. {
  64. if ($valor == 1)
  65. $u = array("", "uma", "duas", "três", "quatro", "cinco", "seis","sete", "oito", "nove");
  66. else
  67. $u = array("", "um", "duas", "três", "quatro", "cinco", "seis","sete", "oito", "nove");
  68.  
  69. $c = array("", "cem", "duzentas", "trezentas", "quatrocentas","quinhentas", "seiscentas", "setecentas", "oitocentas", "novecentas");
  70. }
  71.  
  72. $z = 0;
  73.  
  74. $valor = number_format( $valor, 2, ".", "." );
  75. $inteiro = explode( ".", $valor );
  76.  
  77. for ( $i = 0; $i < count( $inteiro ); $i++ )
  78. for ( $ii = mb_strlen( $inteiro[$i] ); $ii < 3; $ii++ )
  79. $inteiro[$i] = "0" . $inteiro[$i];
  80.  
  81. // $fim identifica onde que deve se dar junção de centenas por "e" ou por "," ;)
  82. $rt = null;
  83. $fim = count( $inteiro ) - ($inteiro[count( $inteiro ) - 1] > 0 ? 1 : 2);
  84. for ( $i = 0; $i < count( $inteiro ); $i++ )
  85. {
  86. $valor = $inteiro[$i];
  87. $rc = (($valor > 100) && ($valor < 200)) ? "cento" : $c[$valor[0]];
  88. $rd = ($valor[1] < 2) ? "" : $d[$valor[1]];
  89. $ru = ($valor > 0) ? (($valor[1] == 1) ? $d10[$valor[2]] : $u[$valor[2]]) : "";
  90.  
  91. $r = $rc . (($rc && ($rd || $ru)) ? " e " : "") . $rd . (($rd && $ru) ? " e " : "") . $ru;
  92. $t = count( $inteiro ) - 1 - $i;
  93. $r .= $r ? " " . ($valor > 1 ? $plural[$t] : $singular[$t]) : "";
  94. if ( $valor == "000")
  95. $z++;
  96. elseif ( $z > 0 )
  97. $z--;
  98.  
  99. if ( ($t == 1) && ($z > 0) && ($inteiro[0] > 0) )
  100. $r .= ( ($z > 1) ? " de " : "") . $plural[$t];
  101.  
  102. if ( $r )
  103. $rt = $rt . ((($i > 0) && ($i <= $fim) && ($inteiro[0] > 0) && ($z < 1)) ? ( ($i < $fim) ? ", " : " e ") : " ") . $r;
  104. }
  105.  
  106. $rt = mb_substr( $rt, 1 );
  107.  
  108. return($rt ? trim( $rt ) : "zero");
  109.  
  110. }
  111. }
  112.  
  113.  
  114. echo Extenso::converte('12,50', true, false);
Add Comment
Please, Sign In to add comment