当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > vfprintf() 函数

PHP
PHP 安全邮件
MySQL 介绍
连接 MySQL
创建 MySQL
MySQL 插入记录
MySQL 选择记录
MySQL Where
MySQL Order By
MySQL 记录更新
MySQL 删除记录
PHP ODBC
XML Expat Parser
XML SimpleXML
PHP 数组参考
PHP Calendar
PHP Date
PHP Directory
PHP Filesystem
PHP FTP
PHP HTTP

PHP 中的 vfprintf() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 577 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The vfprintf() function writes a formatted string to a specified output stream (example: file or database).
vfprintf()函数的作用是:输出格式化字符串到流(例如:文件或数据库)。

Unlike fprintf(), the arguments in vfprintf(), are placed in an array. The array elements will be inserted at the percent (%) signs in the main string. This function works "step-by-step". At the first % sign, the first array element is inserted, at the second % sign, the second array element is inserted, etc.
与fprintf()函数不同,vfprintf()函数中的自变量是位于数组中的,数组元素的字符串之前都要加上百分号(%)。这个函数是“一步一步[step-by-step]”按顺序执行。在第一个%后,将插入第一个数组元素;在第二个%后,将插入第二个数组元素,依次类推。

The vfprintf() function returns the length of the written string.
vfprintf()函数将返回写入的字符串长度。

Syntax
语法

vfprintf(stream,format,argarray)

Parameter参数 Description描述
stream Required. Specifies where to write/output the string
必要参数。指定从哪个位置书写 / 输出字符串
format Required. Specifies the string and how to format the variables in it.
必要参数。指定字符串,以及如何定义其中变量的格式。

Possible format values:
可能值如下:

  • %% - Returns a percent sign
    %% -返回百分号
  • %b - Binary number
    %b –返回二进制数
  • %c - The character according to the ASCII value
    %c –返回与ASCII值相对应的字符
  • %d - Signed decimal number
    %d –带有正负号的十进制数
  • %e - Scientific notation (e.g. 1.2e+2)
    %e –科学计数符号(如:1.2e+2)
  • %u - Unsigned decimal number
    %u –不带正负号的十进制数
  • %f - Floating-point number (local settings aware)
    %f – 浮点数据(本地设置)
  • %F - Floating-point number (not local settings aware)
    %F –浮点数据(非本地设置)
  • %o - Octal number
    %o –十进制数
  • %s - String
    %s –字符串
  • %x - Hexadecimal number (lowercase letters)
    %x –十六进制数(小写字母)
  • %X - Hexadecimal number (uppercase letters)
    %X –十六进制数(大写字母)

Additional format values. These are placed between the % and the letter (example %.2f):
其它格式的值。它是位于%和字母之间的(如:%.2f)

  • + (Forces both + and - in front of numbers. By default, only negative numbers are marked)
    +(在数字前加上+和-;默认情况下,只有负数是被标记出来的)
  • ' (Specifies what to use as padding. Default is space. Must be used together with the width specifier. Example: %'x20s (this uses "x" as padding)
    ’(指定使用什么作为补白,默认值是空格。它必须与宽度指定器一起使用。如:%'x20s(使用“x”作为padding))
  • - (Left-justifies the variable value)
    - (左调整变量值)
  • [0-9] (Specifies the minimum width held of to the variable value)
    [0-9](指定变量值的最小宽度)
  • .[0-9] (Specifies the number of decimal digits or maximum string length)
    .[0-9](指定十进制数值或最大字符串长度)

Note: If multiple additional format values are used, they must be in the same order as above.
注意:如果使用附加格式值,那么它必须与上述顺序相同

argarray Required. An array with arguments to be inserted at the % signs in the format string
必要参数。指定在格式化字符串中插在%之后的带有自变量的数组对象


Tips and Notes
提示和注意点

Note: If there are more % signs than arguments, you must use placeholders. A placeholder is inserted after the % sign, and consists of the argument- number and "$". See example three.
注意:注意:如果这里的%比自变量更多,你必须使用占位符[placeholders]。占位符是安插在%之后的,它是由自变量-数字和“$”组成的。具体可以见案例3。

Tip: Related functions: fprintf(), printf(), sprintf(), vprintf(), and vsprintf().
提示:相关函数:printf(), sprintf(), vfprintf(), vprintf(), 和 vsprintf()


Example 1
案例1

<?php
$str = "Hello";
$number = 123;
$file = fopen("test.txt","w");
echo vfprintf($file,"%s world. Day number %u",array($str,$number));
?> 

The output of the code above will be:
上述代码将输出下面的结果:

27

The following text will be written to the file "test.txt":
下面的文本将被写入文件“text.txt”:

Hello world. Day number 123


Example 2
案例2

<?php
$num1 = 123;
$num2 = 456;
$file = fopen("test.txt","w");
vfprintf($file,"%f%f",array($num1,$num2));
?>

The following text will be written to the file "test.txt":
下面的文本将被写入文件“text.txt”:

123.000000456.000000


Example 3
案例3

Use of placeholders:
使用占位符

<?php
$number = 123;
$file = fopen("test.txt","w");
vfprintf($file,"With 2 decimals: %1$.2f
nWith no decimals: %1$u",array($number));
?>

The following text will be written to the file "test.txt":
下面的文本将被写入文件“text.txt”:

With 2 decimals: 123.00
With no decimals: 123

评论 (0) All

登陆 | 还没注册?