Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: fprintf - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

get_html_translation_table" width="11" height="7"/> <explode
Last updated: Thu, 31 May 2007

view this page in

fprintf

(PHP 5)

fprintf — フォーマットされた文字列をストリームに書き込む

説明

int fprintf ( resource $handle, string $format [, mixed $args [, mixed $...]] )

format によって作成された文字列を handle で指定したストリームに書き込みます。 format については、 sprintf() のドキュメントで説明されています。

出力された文字列の長さを返します。

printf()sprintf()sscanf()fscanf()vsprintf() および number_format() も参照ください。

例 2356. fprintf(): 数値のゼロ埋め

<?php
if (!($fp = fopen('date.txt', 'w')))
    return;

fprintf($fp, "%04d-%02d-%02d", $year, $month, $day);
// ISO 形式にフォーマットした日付を date.txt に書き込みます
?>

例 2357. 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 "$len バイトを currency.txt に書き込みました";
// fprintf の返り値を使用して、書き込まれたバイト数を取得します
?>



add a note add a note User Contributed Notes
fprintf
phyrox at hotmail dot it
08-May-2007 07:23
PHP 4 doesn't support this function, but if you want to use fprintf function or if you have a script wrote for php 5 you can use this function:
<?php
if ( !function_exists('fprintf') ) {
    function
fprintf( $file, $str ) {
       
$argn = func_num_args();
       
$i = strpos( $str, "%" ) + 2;
       
$k = 2;
        while(
$k < $argn  && $i != false ) {
           
$arg = func_get_arg( $k );
           
$str = sprintf( substr( $str , 0 , $i ) , $arg ) . substr( $str, $i );
           
$k = $k + 1;
           
$i = strpos( $str, "%" ) + 2;
        }
        if(
$file != false ) {
            return
fputs( $file, $str, strlen( $str ) );
        } else return
false;
    }
}
?>
jgbreezer at hotmail dot com
07-Sep-2006 11:14
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.
aidan at php dot net
31-May-2004 02: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

 
show source | credits | sitemap | contact | advertising | mirror sites