RE:[PHP-ES] funcion para pasar numeros a letras
| From: | E. Ricardo Santos | Date: | Sat, 06 Mar 2004 21:26:43 +0000 |
| Subject: | RE:[PHP-ES] funcion para pasar numeros a letras | ||
| Groups: | php.general.es | ||
| Request: | Send a blank email to general-es+get-46300@lists.php.net to get a copy of this message | ||
Ya esta migrada y testeada a PHP la funcion que les envie. Les anexo ademas un ejemplo algo
troglodita pero ejemplo al fin:
<?php
// ARCHIVO numletra.php
// funcion give_sql -- realiza las consultas a la base de datos y devuelve
// un objeto con el resultado
function give_sql($STEMP3) {
$mystr = 'SELECT * FROM tblnumletras WHERE numero=' . intval($STEMP3);
$myrs = tep_db_query($mystr);
$rs = tep_db_fetch_object($myrs);
return $rs;
}
// funcion naletras ---- realiza la conversion de los grupos de numeros a palabras
function naletras($NUMERO) {
$STEMP1 = ltrim(rtrim(strval(intval($NUMERO))));
$LARGO = strlen(ltrim(rtrim($STEMP1)));
$STEMP2 = "";
while ($LARGO > 0) {
if (($LARGO == 10) Or ($LARGO == 7) Or ($LARGO == 4) Or ($LARGO == 1)) {
// obtener caracter d ela izq
$STEMP3 = substr(ltrim(rtrim($STEMP1)), 0, 1);
if ($STEMP3 != '0') {
$rs = give_sql($STEMP3);
$NTEMP = $rs->numero;
$SLUGAR2 = $rs->lugar2;
if (!empty($NTEMP)) {
if (($NTEMP != 1) And ($LARGO != 1)) {
$STEMP2 = ltrim(rtrim(ltrim(rtrim($STEMP2)) . " " . $SLUGAR2));
} else {
if (($LARGO != "10") Or ($LARGO != "4")) $STEMP2 = $SLUGAR2;
}
}
if (($LARGO == 10) Or ($LARGO == 4)) $STEMP2 = ltrim(rtrim($STEMP2)) . " MIL";
if ($LARGO == 7) {
if ($STEMP3 == "1") {
$STEMP2 = "UN MILLON ";
} Else {
$STEMP2 = ltrim(rtrim($STEMP2)) . " MILLONES";
}
}
}
}
if (($LARGO == 11) Or ($LARGO == 8) Or ($LARGO == 5) Or ($LARGO == 2)) {
// obtener los dos primeros caracteres de la izq de la cadena
$STEMP3 = substr(ltrim(rtrim($STEMP1)), 0, 2);
if ($STEMP3 != '00') {
$rs = give_sql($STEMP3);
$NTEMP = $rs->numero;
$SLUGAR2 = $rs->lugar2;
if (!empty($NTEMP)) {
$STEMP2 = ltrim(rtrim(ltrim(rtrim($STEMP2)) . " " . $SLUGAR2));
if (($LARGO == 11) Or ($LARGO == 5)) $STEMP2 = ltrim(rtrim($STEMP2)) . " MIL";
if ($LARGO == 8) $STEMP2 = ltrim(rtrim($STEMP2)) . " MILLONES";
$LARGO = $LARGO - 1;
}
}
}
if (($LARGO == 12) Or ($LARGO == 9) Or ($LARGO == 6) Or ($LARGO == 3)) {
$STEMP3 = substr(ltrim(rtrim($STEMP1)), 0, 1);
if ($STEMP3 != "0") {
$rs = give_sql($STEMP3);
$NTEMP = $rs->numero;
$SLUGAR1 = $rs->lugar;
if (!empty($NTEMP)) {
if (($STEMP3 == "1") And (substr(ltrim(rtrim($STEMP1)), 2, 2) == "00")) {
$STEMP2 = ltrim(rtrim($STEMP2)) . " CIEN " . $SLUGAR1;
} Else {
$STEMP2 = ltrim(rtrim($STEMP2)) . " " . $SLUGAR1;
}
}
}
}
$LARGO = $LARGO - 1;
// obtener los caracteres de la derecha
$STEMP1 = substr(ltrim(rtrim($STEMP1)), -$LARGO, $LARGO);
}
return ltrim(rtrim($STEMP2));
}
?>
--------------------------------------------------------------------------------------------------------------------------------
Main que se utilizo para testear la funcion:
<?php
// en este archivo 'application.php' estan mis funciones de manejo de la base de datos
require('includes/application.php');
// Archivo que tiene la funcion de conversion numeros - letras
require('numletra.php');
if (isset($_POST['textfield'])) {
if ($_POST['textfield'] > 99000000) {
echo 'NO SE ADMITEN VALORES MAYORES A 99 MILLONES';
} else {
$myResult = naletras($_POST['textfield']);
echo 'resultado para el numero: ' . $_POST['textfield'] . ' -->
'. $myResult . '<br>';
}
} else {
echo 'ningun resultado' . '<br>';
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Documento sin título</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" method="post" action="prueba_nl.php">
<input type="text" name="textfield">
<input type="submit" name="Submit" value="Enviar">
</form>
<p> </p>
</body>
</html>
Espero les sea de ayuda.
Saludos y suerte
--------------------------------------
100% Cubano!!!