<?php
 //$cMontext="";
 function NumPalabra($Numero, $SinPeso){
  $Numerofmt="";
  $centenas=0;
  $pos=0;
  $cen=0;
  $dec=0;
  $uni=0;
  $textuni="";
  $textdec="";
  $textcen="";
  $milestxt="";
  $monedatxt="";
  $txtPalabra="";
  $Numerofmt="";//sprintf("%016d",$Numerofmt);// 'Le da un formato fijo
  $centenas = 1;
  $pos = 1;
  $txtPalabra = "";
  while ($centenas <= 5){
   //' extrae series de Centena, Decena, Unidad
   // Falta tranforma de cadena a numero
   $cen = substr($Numerofmt,$pos,1);
//   $cen = trim((string)@$cen);
   $dec = substr($Numerofmt,$pos+1,1);
   $uni = substr($Numerofmt,$pos+2,1);
   $pos = $pos + 3;
   $textcen = Centena($uni, $dec, $cen);
   $textdec = Decena($uni, $dec);
   $textuni = Unidad($uni, $dec);
   // determina separador de miles/millones
   switch ($centenas){
   	case 1:
//	 $otro=$cen + $dec + $uni;
	 if ($cen + $dec + $uni == 1){
	 //if ($otro == 1){
	  $milestxt = "Billon ";
	  }elseif ($cen + $dec + $uni > 1){
	   $milestxt = "Billones ";
	 }
	case 2:
	 $otro2=substr($Numerofmt,7, 3);
	 //if ($cen + $dec + $uni) >= 1 and substr($Numerofmt,7, 3) == 0) {
	 if (($cen + $dec + $uni >= 1) && (substr($Numerofmt,7, 3) == 0)) {
	  $milestxt = "Mil Millones ";
	  }elseif($cen + $dec + $uni >= 1) {
	   $milestxt = "Mil ";
	 } 
	case 3:
	 if (($cen + $dec) == 0 && ($uni) == 1) {
	  $milestxt = "Millon ";
	  }elseif (($cen > 0) || ($dec > 0) || ($uni > 1)) {
	   $milestxt = "Millones ";
	 }
	case 4:
	  if (($cen + $dec + $uni) >= 1){
	   $milestxt = "Mil ";
	  }
	case 5:
	  if (($cen + $dec + $uni) >= 1) {
	   $milestxt = "";
	  }
   }// fin de switch
   $centenas = $centenas + 1;
   //va formando el texto del importe en palabras
   $txtPalabra = $txtPalabra + $textcen + $textdec + $textuni + $milestxt;
   $milestxt = "";
   $textuni = "";
   $textdec = "";
   $textcen = "";
  }// fin del while 
  //agrega denominacion de moneda
  switch ($Numerofmt){
   case 0:
    $monedatxt = "Cero Pesos ";
   case 1:
    $monedatxt = "Peso ";
   case $Numerofmt < 1000000:
	$monedatxt = "Pesos ";
   case $Numerofmt >= 1000000: 
    $monedatxt = "de Pesos ";
  }
  if ($SinPeso == true){
   $NumPalabra = txtPalabra;
   }else{
    $txtPalabra = $txtPalabra . $monedatxt . 'con ' . substr($Numerofmt,17) . '/100';
	$NumPalabra = $txtPalabra;
  }
  //Return txtPalabra.ToUpper
 }//fin Function
 function Centena($uni, $dec, $cen){
  switch ($cen){
   case 1:
    if (($dec + $uni) == 0){
	 $cMontext = "cien ";
	 }else{
	  $cMontext = "ciento ";
	}
   case 2:
    $cMontext = "doscientos ";
   case 3:
    $cMontext = "trescientos ";
   case 4:
   $cMontext = "cuatrocientos ";
	case 5:
	 $cMontext = "quinientos ";
	case 6:
	 $cMontext = "seiscientos ";
	case 7: 
	 $cMontext = "setecientos ";
	case 8:
	 $cMontext = "ochocientos ";
	case 9:
	 $cMontext = "novecientos ";
	default:
	 $cMontext = "";
   }// end swith
   //Centena = $cMontext;
   return ($cMontext);
   $cMontext = "";
 }//End Function
 function Decena($uni,$dec){
  switch ($dec){
   case 1:
    switch ($uni){
	 case 0: $cMontext = "diez ";
	 case 1: $cMontext = "once ";
	 case 2: $cMontext = "doce ";
	 case 3: $cMontext = "trece ";
	 case 4: $cMontext = "catorce ";
	 case 5: $cMontext = "quince ";
	 case 6: $cMontext = "dieci";
	 case 7: $cMontext = "dieci";
	 case 8: $cMontext = "dieci";
	 case 9: $cMontext = "dieci";
	 //To || 7 || 8 || 9: cMontext = "dieci";
	}
	case 2:
	 if ($uni == 0){
	  $cMontext = "veinte ";
	  }elseif ($uni > 0){
	   $cMontext = "veinti";
	 }
	 case 3: $cMontext = "treinta ";
	 case 4: $cMontext = "cuarenta ";
	 case 5: $cMontext = "cincuenta ";
	 case 6: $cMontext = "sesenta ";
	 case 7: $cMontext = "setenta ";
     case 8: $cMontext = "ochenta ";
	 case 9: $cMontext = "noventa ";
	 default: $cMontext = "";
   } //End Select
   if (($uni > 0) && ($dec > 2)){ //
    $cMontext = cMontext + "y ";
   }
   return ($cMontext);
   //Decena = $cMontext;
   $cMontext = "";
  }//End Function
 function Unidad($uni,$dec){
  if ($dec <> 1){
   switch ($uni){
    case 1: $cMontext = "un ";
	case 2: $cMontext = "dos ";
	case 3: $cMontext = "tres ";
	case 4: $cMontext = "cuatro ";
	case 5: $cMontext = "cinco ";
   }
  }//End If
  switch ($uni){
   case 6: $cMontext = "seis ";
   case 7: $cMontext = "siete ";
   case 8: $cMontext = "ocho ";
   case 9: $cMontext = "nueve ";
  }//  End Select
  //  Unidad = $cMontext;
  return ($cMontext);
  $cMontext = "";
 }
echo NumPalabra('121210020','peso'). " pesos" ;
?>