首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

sprintf

(PHP 4, PHP 5, PHP 7)

sprintf - 返回格式化的字符串

描述

代码语言:javascript
复制
string sprintf ( string $format [, mixed $args [, mixed $... ]] )

返回根据格式化字符串格式生成的字符串。

参数

format

格式字符串由零个或多个指令构成:直接复制到结果和转换规范的普通字符(不包括),每个规则都会导致获取自己的参数。这适用于sprintf()和printf()。

每个转换规范都包含一个百分号(),后跟一个或多个这些元素,依次为:

  • 强制在数字上使用符号( - 或+)的可选符号说明符。默认情况下,如果数字为负数,则仅在数字上使用 - 符号。此说明符强制正数以及附加+号。
  • 一个可选的填充说明符,说明将使用哪个字符将结果填充到正确的字符串大小。这可能是空格字符或0(零字符)。默认是填充空格。可以通过用单引号(')作为前缀来指定备用填充字符。看下面的例子。
  • 一个可选的对齐说明符,说明结果应该是左对齐还是右对齐。默认值是右对齐的; 一个-这里字符将使它左对齐。
  • 一个可选数字,一个宽度说明符,表示该转换应该产生多少个字符(最小值)。
  • 一个可选的精度说明符,其格式为句点(),后跟一个可选的十进制数字字符串,表示应该为浮点数显示多少个小数位数。在字符串上使用此说明符时,它充当截止点,为字符串设置最大字符限制。此外,填充数字时使用的字符可以选择在句点和数字之间指定。
  • 一个类型说明符,说明参数数据应被视为什么类型。可能的类型:
代码语言:txt
复制
-  _%_ - a literal percent character. No argument is required. 
-  _b_ - the argument is treated as an integer and presented as a binary number. 
-  _c_ - the argument is treated as an integer and presented as the character with that ASCII value. 
-  _d_ - the argument is treated as an integer and presented as a (signed) decimal number. 
-  _e_ - the argument is treated as scientific notation (e.g. 1.2e+2). The precision specifier stands for the number of digits after the decimal point since PHP 5.2.1. In earlier versions, it was taken as number of significant digits (one less). 
-  _E_ - like _%e_ but uses uppercase letter (e.g. 1.2E+2). 
-  _f_ - the argument is treated as a float and presented as a floating-point number (locale aware). 
-  _F_ - the argument is treated as a float and presented as a floating-point number (non-locale aware). Available since PHP 5.0.3. 
-  _g_ - shorter of _%e_ and _%f_. 
-  _G_ - shorter of _%E_ and _%f_. 
-  _o_ - the argument is treated as an integer and presented as an octal number. 
-  _s_ - the argument is treated as and presented as a string. 
-  _u_ - the argument is treated as an integer and presented as an unsigned decimal number. 
-  _x_ - the argument is treated as an integer and presented as a hexadecimal number (with lowercase letters). 
-  _X_ - the argument is treated as an integer and presented as a hexadecimal number (with uppercase letters). 

变量将被合成为指定符合适的类型:

类型

说明符

小号

整数

d,u,c,o,x,X,b

g,G,e,E,f,F

警告

试图将字符串和宽度说明符与字符集组合使用,每个字符需要多个字节可能会导致意外的结果

格式字符串支持参数编号/交换。这里是一个例子:

示例#1参数交换

代码语言:javascript
复制
<?php
$num?=?5;
$location?=?'tree';

$format?=?'There?are?%d?monkeys?in?the?%s';
echo?sprintf($format,?$num,?$location);
?>

这将输出“There are 5 monkeys in the tree”。但想象一下,我们在一个单独的文件中创建了一个格式化字符串,通常是因为我们想将其国际化并将其重写为:

示例#2 参数交换

代码语言:javascript
复制
<?php
$format?=?'The?%s?contains?%d?monkeys';
echo?sprintf($format,?$num,?$location);
?>

我们现在有一个问题。格式字符串中占位符的顺序与代码中参数的顺序不匹配。我们希望保持原样,并简单地在格式字符串中指明占位符引用的参数。我们会这样写格式字符串:

示例#3 参数交换

代码语言:javascript
复制
<?php
$format?=?'The?%2$s?contains?%1$d?monkeys';
echo?sprintf($format,?$num,?$location);
?>

这里的另一个好处是,您可以在代码中不添加更多参数的情况下重复占位符。例如:

示例#4 参数交换

代码语言:javascript
复制
<?php
$format?=?'The?%2$s?contains?%1$d?monkeys.
???????????That\'s?a?nice?%2$s?full?of?%1$d?monkeys.';
echo?sprintf($format,?$num,?$location);
?>

在使用参数交换时,n $ 位置说明符必须紧跟在百分号()之后,位于任何其他说明符之前,如下例所示。

示例#5 指定填充字符

代码语言:javascript
复制
<?php
echo?sprintf("%'.9d\n",?123);
echo?sprintf("%'.09d\n",?123);
?>

上面的例子将输出:

代码语言:javascript
复制
......123
000000123

例#6 使用其他说明符的位置说明符

代码语言:javascript
复制
<?php
$format?=?'The?%2$s?contains?%1$04d?monkeys';
echo?sprintf($format,?$num,?$location);
?>

上面的例子将输出:

代码语言:javascript
复制
The tree contains 0005 monkeys

注意:试图使用大于的位置说明符PHP_INT_MAX会导致sprintf()生成警告。

警告

所述? 类型说明符忽略填充和宽度

args ...

返回值

返回根据格式化字符串格式生成的字符串,或在失败时返回FALSE。

例子

示例#7 printf():各种示例

代码语言:javascript
复制
<?php
$n?=??43951789;
$u?=?-43951789;
$c?=?65;?//?ASCII?65?is?'A'

//?notice?the?double?%%,?this?prints?a?literal?'%'?character
printf("%%b?=?'%b'\n",?$n);?//?binary?representation
printf("%%c?=?'%c'\n",?$c);?//?print?the?ascii?character,?same?as?chr()?function
printf("%%d?=?'%d'\n",?$n);?//?standard?integer?representation
printf("%%e?=?'%e'\n",?$n);?//?scientific?notation
printf("%%u?=?'%u'\n",?$n);?//?unsigned?integer?representation?of?a?positive?integer
printf("%%u?=?'%u'\n",?$u);?//?unsigned?integer?representation?of?a?negative?integer
printf("%%f?=?'%f'\n",?$n);?//?floating?point?representation
printf("%%o?=?'%o'\n",?$n);?//?octal?representation
printf("%%s?=?'%s'\n",?$n);?//?string?representation
printf("%%x?=?'%x'\n",?$n);?//?hexadecimal?representation?(lower-case)
printf("%%X?=?'%X'\n",?$n);?//?hexadecimal?representation?(upper-case)

printf("%%+d?=?'%+d'\n",?$n);?//?sign?specifier?on?a?positive?integer
printf("%%+d?=?'%+d'\n",?$u);?//?sign?specifier?on?a?negative?integer
?>

上面的例子将输出:

代码语言:javascript
复制
%b = '10100111101010011010101101'
%c = 'A'
%d = '43951789'
%e = '4.39518e+7'
%u = '43951789'
%u = '4251015507'
%f = '43951789.000000'
%o = '247523255'
%s = '43951789'
%x = '29ea6ad'
%X = '29EA6AD'
%+d = '+43951789'
%+d = '-43951789'

示例#8 printf():字符串说明符

代码语言:javascript
复制
<?php
$s?=?'monkey';
$t?=?'many?monkeys';

printf("[%s]\n",??????$s);?//?standard?string?output
printf("[%10s]\n",????$s);?//?right-justification?with?spaces
printf("[%-10s]\n",???$s);?//?left-justification?with?spaces
printf("[%010s]\n",???$s);?//?zero-padding?works?on?strings?too
printf("[%'#10s]\n",??$s);?//?use?the?custom?padding?character?'#'
printf("[%10.10s]\n",?$t);?//?left-justification?but?with?a?cutoff?of?10?characters
?>

上面的例子将输出:

代码语言:javascript
复制
[monkey]
[    monkey]
[monkey    ]
[0000monkey]
[####monkey]
[many monke]

示例#9 sprintf():零填充整数

代码语言:javascript
复制
<?php
$isodate?=?sprintf("%04d-%02d-%02d",?$year,?$month,?$day);
?>

示例#10 sprintf():格式化货币

代码语言:javascript
复制
<?php
$money1?=?68.75;
$money2?=?54.35;
$money?=?$money1?+?$money2;
//?echo?$money?will?output?"123.1";
$formatted?=?sprintf("%01.2f",?$money);
//?echo?$formatted?will?output?"123.10"
?>

示例#11 sprintf():科学记数法

代码语言:javascript
复制
<?php
$number?=?362525200;

echo?sprintf("%.3e",?$number);?//?outputs?3.625e+8
?>

扩展内容

  • printf() - 输出格式化的字符串
  • sscanf() - 根据格式解析字符串的输入
  • fscanf() - 根据格式解析文件的输入
  • vsprintf() - 返回格式化的字符串
  • number_format() - 用分组数千格式化数字

← soundex

sscanf →

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com