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(): 0을 채운 정수
<?php
if (!($fp = fopen('date.txt', 'w'))) {
return;
}
fprintf($fp, "%04d-%02d-%02d", $year, $month, $day);
// date.txt에 형식화한 ISO 날짜를 기록합니다
?>
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);
// currency.txt에 "123.10"을 씁니다
echo "wrote $len bytes to currency.txt";
// fprintf의 반환값은 기록한 바이트를 확인할 때 사용합니다
?>
참고
- printf() - 형식화한 문자열을 출력
- sprintf() - 형식화한 문자열을 반환
- sscanf() - 문자열을 형식에 따라 해석
- fscanf() - Parses input from a file according to a format
- vsprintf() - Return a formatted string
- number_format() - Format a number with grouped thousands
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
