fprintf 将格式化后的字符串写入到流
发表日期:2021-07-01 10:23:20 | 来源: | | 浏览(1569) 分类:字符串 函数
fprintf
(PHP 5, PHP 7, PHP 8)
fprintf — 将格式化后的字符串写入到流
说明
$handle, string $format, mixed $... = ?): int
写入一个根据 format 格式化后的字符串到
由 handle 句柄打开的流中。
参数
-
handle -
文件系统指针,是典型地由 fopen() 创建的 resource(资源)。
-
format -
The format string is composed of zero or more directives: ordinary characters (excluding
%) that are copied directly to the result and conversion specifications, each of which results in fetching its own parameter.A conversion specification follows this prototype:
%[argnum$][flags][width][.precision]specifier.Argnum
An integer followed by a dollar sign
$, to specify which number argument to treat in the conversion.Flags Flag 说明 -Left-justify within the given field width; Right justification is the default +Prefix positive numbers with a plus sign +; Default only negative are prefixed with a negative sign.(space)Pads the result with spaces. This is the default. 0Only left-pads numbers with zeros. With sspecifiers this can also right-pad with zeros.'(char)Pads the result with the character (char). Width
An integer that says how many characters (minimum) this conversion should result in.
Precision
A period
.followed by an integer who's meaning depends on the specifier:-
For
e,E,fandFspecifiers: this is the number of digits to be printed after the decimal point (by default, this is 6). -
For
g,G,handHspecifiers: this is the maximum number of significant digits to be printed. -
For
sspecifier: it acts as a cutoff point, setting a maximum character limit to the string.
注意: If the period is specified without an explicit value for precision, 0 is assumed.
注意: Attempting to use a position specifier greater than
PHP_INT_MAXwill generate warnings.Specifiers Specifier 说明 %A literal percent character. No argument is required. bThe argument is treated as an integer and presented as a binary number. cThe argument is treated as an integer and presented as the character with that ASCII. dThe argument is treated as an integer and presented as a (signed) decimal number. eThe argument is treated as scientific notation (e.g. 1.2e+2). ELike the especifier but uses uppercase letter (e.g. 1.2E+2).fThe argument is treated as a float and presented as a floating-point number (locale aware). FThe argument is treated as a float and presented as a floating-point number (non-locale aware). gGeneral format.
Let P equal the precision if nonzero, 6 if the precision is omitted, or 1 if the precision is zero. Then, if a conversion with style E would have an exponent of X:
If P > X ≥ −4, the conversion is with style f and precision P − (X + 1). Otherwise, the conversion is with style e and precision P − 1.
GLike the gspecifier but usesEandf.hLike the gspecifier but usesF. Available as of PHP 8.0.0.HLike the gspecifier but usesEandF. Available as of PHP 8.0.0.oThe argument is treated as an integer and presented as an octal number. sThe argument is treated and presented as a string. uThe argument is treated as an integer and presented as an unsigned decimal number. xThe argument is treated as an integer and presented as a hexadecimal number (with lowercase letters). XThe argument is treated as an integer and presented as a hexadecimal number (with uppercase letters). 警告The
ctype specifier ignores padding and width警告Attempting to use a combination of the string and width specifiers with character sets that require more than one byte per character may result in unexpected results
Variables will be co-erced to a suitable type for the specifier:
Type Handling Type Specifiers string sint d,u,c,o,x,X,bfloat e,E,f,F,g,G,h,H -
For
-
... -
返回值
返回写入的字符串长度。
范例
示例 #1 fprintf(): zero-padded integers
<?php
if (!($fp = fopen('date.txt', 'w'))) {
return;
}
fprintf($fp, "%04d-%02d-%02d", $year, $month, $day);
// will write the formatted ISO date to date.txt?>
示例 #2 fprintf(): formatting currency
<?php
if (!($fp = fopen('currency.txt', 'w'))) {
return;
}
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money will output "123.1";
$len = fprintf($fp, '%01.2f', $money);
// will write "123.10" to currency.txtecho "wrote $len bytes to currency.txt";
// use the return value of fprintf to determine how many bytes we wrote?>
参见
- printf() - 输出格式化字符串
- sprintf() - 返回格式化字符串
- vprintf() - 输出格式化字符串
- vsprintf() - 返回格式化字符串
- vfprintf() - 将格式化字符串写入流
- sscanf() - 根据指定格式解析输入的字符
- fscanf() - 从文件中格式化输入
- number_format() - 以千位分隔符方式格式化一个数字
- date() - 格式化一个本地时间/日期
- PHP(0)
- PHP杂项(34)
- PHP基础-李炎恢系列课程(20)
- 中文函数手册(0)
- 错误处理 函数(13)
- OPcache 函数(6)
- PHP 选项/信息 函数(54)
- Zip 函数(10)
- Hash 函数(15)
- OpenSSL 函数(63)
- Date/Time 函数(51)
- 目录函数(9)
- Fileinfo 函数(6)
- iconv 函数(11)
- 文件系统函数(81)
- 多字节字符串 函数(57)
- GD 和图像处理 函数(114)
- 可交换图像信息(5)
- Math 函数(50)
- 程序执行函数(11)
- PCNTL 函数(23)
- JSON 函数(4)
- SPL 函数(15)
- URL 函数(10)
- cURL 函数(32)
- 网络 函数(33)
- FTP 函数(36)
- Session 函数(23)
- PCRE 函数(11)
- PCRE 正则语法(19)
- 数组 函数(81)
- 类/对象 函数(18)
- 函数处理 函数(13)
- 变量处理 函数(37)
- SimpleXML 函数(3)
- 杂项 函数(31)
- 字符串 函数(101)
- addcslashes 以 C 语言风格使用反斜线转义字符串中的字符(0)
- addslashes 使用反斜线引用字符串(0)
- bin2hex 函数把包含数据的二进制字符串转换为十六进制值(0)
- chop rtrim() 的别名(0)
- chr 返回指定的字符(0)
- chunk_split 将字符串分割成小块(0)
- convert_cyr_string 将字符由一种 Cyrillic 字符转换成另一种(0)
- convert_uudecode 解码一个 uuencode 编码的字符串(0)
- convert_uuencode 使用 uuencode 编码一个字符串(0)
- count_chars 返回字符串所用字符的信息(0)
- crc32 计算一个字符串的 crc32 多项式(0)
- crypt 单向字符串散列(0)
- echo 输出一个或多个字符串(0)
- explode 使用一个字符串分割另一个字符串(0)
- fprintf 将格式化后的字符串写入到流(0)
- get_html_translation_table 返回使用 htmlspecialchars() 和 htmlentities() 后的转换表(0)
- hebrev 将逻辑顺序希伯来文(logical-Hebrew)转换为视觉顺序希伯来文(visual-Hebrew)(0)
- hebrevc 将逻辑顺序希伯来文(logical-Hebrew)转换为视觉顺序希伯来文(visual-Hebrew),并且转换换行符(0)
- hex2bin 转换十六进制字符串为二进制字符串(0)
- html_entity_decode Convert HTML entities to their corresponding characters(0)
- htmlentities 将字符转换为 HTML 转义字符(0)
- htmlspecialchars_decode 将特殊的 HTML 实体转换回普通字符(0)
- htmlspecialchars 将特殊字符转换为 HTML 实体(0)
- implode 将一个一维数组的值转化为字符串(0)
- join 别名 implode()(0)
- lcfirst 使一个字符串的第一个字符小写(0)
- levenshtein 计算两个字符串之间的编辑距离(0)
- localeconv Get numeric formatting information(0)
- ltrim 删除字符串开头的空白字符(或其他字符)(0)
- md5_file 计算指定文件的 MD5 散列值(0)
- md5 计算字符串的 MD5 散列值(0)
- metaphone Calculate the metaphone key of a string(0)
- money_format 将数字格式化成货币字符串(0)
- nl_langinfo Query language and locale information(0)
- nl2br 在字符串所有新行之前插入 HTML 换行标记(0)
- number_format 以千位分隔符方式格式化一个数字(0)
- ord 转换字符串第一个字节为 0-255 之间的值(0)
- parse_str 将字符串解析成多个变量(0)
- print 输出字符串(0)
- printf 输出格式化字符串(0)
- quoted_printable_decode 将 quoted-printable 字符串转换为 8-bit 字符串(0)
- quoted_printable_encode 将 8-bit 字符串转换成 quoted-printable 字符串(0)
- quotemeta 转义元字符集(0)
- rtrim 删除字符串末端的空白字符(或者其他字符)(0)
- setlocale 设置地区信息(0)
- sha1_file 计算文件的 sha1 散列值(0)
- sha1 计算字符串的 sha1 散列值(0)
- similar_text 计算两个字符串的相似度(0)
- soundex Calculate the soundex key of a string(0)
- sprintf 返回格式化字符串(0)
- sscanf 根据指定格式解析输入的字符(0)
- str_contains Determine if a string contains a given substring(0)
- str_ends_with Checks if a string ends with a given substring(0)
- str_getcsv 解析 CSV 字符串为一个数组(0)
- str_ireplace str_replace() 的忽略大小写版本(0)
- str_pad 使用另一个字符串填充字符串为指定长度(0)
- str_repeat 重复一个字符串(0)
- str_replace 子字符串替换(0)
- str_rot13 对字符串执行 ROT13 转换(0)
- str_shuffle 随机打乱一个字符串(0)
- str_split 将字符串转换为数组(0)
- str_starts_with Checks if a string starts with a given substring(0)
- str_word_count 返回字符串中单词的使用情况(0)
- strcasecmp 二进制安全比较字符串(不区分大小写)(0)
- strchr 别名 strstr()(0)
- strcmp 二进制安全字符串比较(0)
- strcoll 基于区域设置的字符串比较(0)
- strcspn 获取不匹配遮罩的起始子字符串的长度(0)
- strip_tags 从字符串中去除 HTML 和 PHP 标记(0)
- stripcslashes 反引用一个使用 addcslashes() 转义的字符串(0)
- stripos 查找字符串首次出现的位置(不区分大小写)(0)
- stripslashes 反引用一个引用字符串(0)
- stristr strstr() 函数的忽略大小写版本(0)
- strlen 获取字符串长度(0)
- strnatcasecmp 使用“自然顺序”算法比较字符串(不区分大小写)(0)
- strnatcmp 使用自然排序算法比较字符串(0)
- strncasecmp 二进制安全比较字符串开头的若干个字符(不区分大小写)(0)
- strncmp 二进制安全比较字符串开头的若干个字符(0)
- strpbrk 在字符串中查找一组字符的任何一个字符(0)
- strpos 查找字符串首次出现的位置(0)
- strrchr 查找指定字符在字符串中的最后一次出现(0)
- strrev 反转字符串(0)
- strripos 计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写)(0)
- strrpos 计算指定字符串在目标字符串中最后一次出现的位置(0)
- strspn 计算字符串中全部字符都存在于指定字符集合中的第一段子串的长度。(0)
- strstr 查找字符串的首次出现(0)
- strtok 标记分割字符串(0)
- strtolower 将字符串转化为小写(0)
- strtoupper 将字符串转化为大写(0)
- strtr 转换指定字符(0)
- substr_compare 二进制安全比较字符串(从偏移位置比较指定长度)(0)
- substr_count 计算字串出现的次数(0)
- substr_replace 替换字符串的子串(0)
- substr 返回字符串的子串(0)
- trim 去除字符串首尾处的空白字符(或者其他字符)(0)
- ucfirst 将字符串的首字母转换为大写(0)
- ucwords 将字符串中每个单词的首字母转换为大写(0)
- vfprintf 将格式化字符串写入流(0)
- vprintf 输出格式化字符串(0)
- vsprintf 返回格式化字符串(0)
- wordwrap 打断字符串为指定数量的字串(0)
宁公网安备 64010402001209号