Another alternative using sprintf and fwrite() for pre-v5 php's:
fwrite( resource, sprintf(format [, mixed args [, mixed ...]] ))
Barring slight logical differences in meaning of returned value and (maybe??) how it handles magic_quotes_runtime config option, see fwrite() help.
fprintf
(PHP 5)
fprintf — Escribir una cadena con formato a una secuencia
Descripción
Escribe una cadena producida de acuerdo a formato al recurso de secuencia especificado por gestor .
Valores retornados
Devuelve la longitud de la cadena impresa.
Ejemplos
Example #1 fprintf(): enteros con relleno de ceros
<?php
if (!($da = fopen('fecha.txt', 'w')))
return;
fprintf($da, "%04d-%02d-%02d", $anyo, $mes, $dia);
// escribe la fecha en formato ISO a fecha.txt
?>
Example #2 fprintf(): dando formato a valores monetarios
<?php
if (!($da = fopen('monetario.txt', 'w')))
return;
$dinero1 = 68.75;
$dinero2 = 54.35;
$dinero = $dinero1 + $dinero2;
// echo $dinero producirá la salida "123.1";
$long = fprintf($da, '%01.2f', $dinero);
// escribe "123.10" en monetario.txt
echo "se escribieron $long bytes a monetario.txt";
// usar el valor de retorno para determinar cuántos bytes se escribieron
?>
fprintf
jgbreezer at hotmail dot com
07-Sep-2006 07:14
07-Sep-2006 07:14
aidan at php dot net
30-May-2004 10:35
30-May-2004 10:35
This functionality is now implemented in the PEAR package PHP_Compat.
More information about using this function without upgrading your version of PHP can be found on the below link:
http://pear.php.net/package/PHP_Compat
