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 — Записва форматиран низ в поток
Описание
Записва низ, създаден на базата на параметъра format , в поточен ресурс указан от handle .
Параметри
Връщани стойности
Връща дължината на записания низ.
Примери
Example #1 fprintf(): запълване с нули
<?php
if (!($fp = fopen('date.txt', 'w'))) {
return;
}
fprintf($fp, "%04d-%02d-%02d", $year, $month, $day);
// ще запише форматирана ISO дата в date.txt
?>
Example #2 fprintf(): форматиране на парични величини
<?php
if (!($fp = fopen('currency.txt', 'w'))) {
return;
}
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money ще изведе "123.1";
$len = fprintf($fp, '%01.2f', $money);
// ще запише "123.10" в currency.txt
echo "wrote $len bytes to currency.txt";
// използвайте стойността, която връща fprintf за да определите колко байта са записани
?>
fprintf
jgbreezer at hotmail dot com
07-Sep-2006 04:14
07-Sep-2006 04:14
aidan at php dot net
30-May-2004 07:35
30-May-2004 07: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
