php中sprintf()函数用法详解
来源:互联网 发布:mac删除迅雷 编辑:程序博客网 时间:2024/05/01 19:04
sprintf(format,arg1,arg2,arg++)
sprintf()函数的作用是:输出格式化字符串到变量。(注意这里是返回值,而不是输出值)
arg1, arg2, ++参数将被插入到主体字符串中的百分号(%)之后。这个函数是“一步一步[step-by-step]”执行的。在第一个“%”之后插入arg1,在第二个“%”之后插入arg2,依次类推。
语法:
Parameter参数Description描述formatRequired. Specifies the string and how to format the variables in it.
必要参数。指定字符串,以及如何定义其中变量的格式。Possible format values:
可能值如下:
Additional format values. These are placed between the % and the letter (example %.2f):
%% -返回百分号
%b –返回二进制数
%c –返回与ASCII值相对应的字符
%d –带有正负号的十进制数
%e –科学计数符号(如:1.2e+2)
%u –不带正负号的十进制数
%f – 浮点数据(本地设置)
%F –浮点数据(非本地设置)
%o –十进制数
%s –字符串
%x –十六进制数(小写字母)
%X –十六进制数(大写字母)
其它格式的值。它是位于%和字母之间的(如:%.2f)
Note:
+(在数字前加上+和-;默认情况下,只有负数是被标记出来的)
’(指定使用什么作为补白,默认值是空格。它必须与宽度指定器一起使用。如:%’x20s(使用“x”作为padding))
- (左调整变量值)
[0-9](指定变量值的最小宽度)
.[0-9](指定十进制数值或最大字符串长度)
If multiple additional format values are used, they must be in the same order as above.
注意:如果使用附加格式值,那么它必须与上述顺序相同arg1Required. The argument to be inserted at the first %-sign in the format string
必要参数。这个自变量(arg1)必须安插在第一个%-符号前arg2Optional. The argument to be inserted at the second %-sign in the format string
可选参数。这个自变量(arg2)必须安插在第二个%-符号前arg++Optional. The argument to be inserted at the third, fourth, etc. %-sign in the format string
可选参数。与上述自变量相同,它们可以安插在第三个、第四个……(依次类推)%-符号前。
提示和注意点:
注意:如果这里的%比自变量更多,你必须使用占位符[placeholders]。占位符是安插在%之后的,它是由自变量-数字和“/$”组成的。具体如下:代码:
<?php
$number = 123;
$txt = sprintf("With 2 decimals: %1/$.2f<br />With no decimals: %1/$u",$number);
echo $txt;
?>
With 2 decimals: 123.00 With no decimals: 123
- php中sprintf()函数用法详解
- php sprintf用法详解
- PHP sprintf() 函数用法
- sprintf函数用法详解
- PHP sprintf()函数详解
- [转]sprintf函数用法详解
- PHP函数printf()、sprintf()的用法
- PHP函数printf()、sprintf()的用法
- php sprintf 函数最新详解--转
- C语言中sprintf用法详解
- C语言中sprintf用法详解
- PHP中 sprintf()格式化输出详解
- C++中 sprintf函数的用法
- C语言中sprintf()函数的用法
- C语言中sprintf()函数的用法
- C语言中sprintf()函数的用法
- loadrunner中sprintf函数的用法
- C语言中sprintf()函数的用法
- 解决 jQuery 与 prototype冲突 jQuery与easyvalidation冲突
- linux进程管理之信号处理(1)
- linux进程管理之信号处理(1)
- Flex:通过describeType(value:*)取得一个类的别名
- [构建自己的持久层]四、Property对象属性描述
- php中sprintf()函数用法详解
- Android实现自定义菜单
- JAVA中String与StringBuffer的区别
- 必知:嵌入式系统学习步骤
- 主题:window窗体对象open()和showModalDialog()用法
- VC中ListCtrl经验总结【转载】
- HDC与CDC的相互转换
- SQL Server嵌套事务探讨 (转贴)
- loadrunner学习笔记(从MYSQL中取数据)