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 で指定したストリームに書き込みます。
パラメータ
返り値
書き込まれた文字列の長さを返します。
例
例1 fprintf(): 数値のゼロ埋め
<?php
if (!($fp = fopen('currency.txt', 'w'))) {
return;
}
fprintf($fp, "%04d-%02d-%02d", $year, $month, $day);
// ISO 形式にフォーマットした日付を date.txt に書き込みます
?>
例2 fprintf(): 金額のフォーマット
<?php
if (!($fp = fopen('date.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 "$len バイトを currency.txt に書き込みました";
// fprintf の返り値を使用して、書き込まれたバイト数を取得します
?>
参考
- printf() - フォーマット済みの文字列を出力する
- sprintf() - フォーマットされた文字列を返す
- sscanf() - フォーマット文字列に基づき入力を処理する
- fscanf() - フォーマットに基づきファイルからの入力を処理する
- vsprintf() - フォーマットされた文字列を返す
- number_format() - 数字を千位毎にグループ化してフォーマットする
fprintf
jgbreezer at hotmail dot com
07-Sep-2006 02:14
07-Sep-2006 02:14
aidan at php dot net
30-May-2004 05:35
30-May-2004 05: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
