-
11月10日
-
【函数功能】
该函数把格式化的字符串写入到变量中。
【函数语法】
sprintf ($format, $args = null, $_ = null)
【参数说明】
$format:必需:字符串以及如何格式化其中的变量。 变量可能的格式值: %% - 返回一个百分号 % %b - 二进制数 %c - ASCII 值对应的字符 %d - 包含正负号的十进制数(负数、0、正数) %e - 使用小写的科学计数法(例如 1.2e+2) %E - 使用大写的科学计数法(例如 1.2E+2) %u - 不包含正负号的十进制数(大于等于 0) %f - 浮点数(本地设置) %F - 浮点数(非本地设置) %g - 较短的 %e 和 %f %G - 较短的 %E 和 %f %o - 八进制数 %s - 字符串 %x - 十六进制数(小写字母) %X - 十六进制数(大写字母) 附加的格式值。必需放置在 % 和字母之间(例如 %.2f): + (在数字前面加上 + 或 - 来定义数字的正负性。 默认地,只有负数才做标记,正数不做标记) ' (规定使用什么作为填充,默认是空格。它必须与宽度指定器一起使用。 例如:%'x20s(使用 "x" 作为填充)) - (左调整变量值) [0-9] (规定变量值的最小宽度) .[0-9] (规定小数位数或最大字符串长度) 注释:如果使用多个额外的格式值,则必须按照上面的顺序进行使用。 $args:必需,插到 format 字符串中第一个 % 符号处的参数。 $_:可选。规定插到 format 字符串中第N个 % 符号处的参数。
【演示程序】
<?php /** * sprintf ($format, $args = null, $_ = null) * **/ $format = "test sprintf function:100%% useful,%b binary,%c ASCII,%d number,%e big number,%.2f float,%'x20s"; echo sprintf($format,100,98,1234,99999999999,-10,"and so on!"); ?>
【输出结果】
test sprintf function:100% useful,1100100 binary,b ASCII,1234 number,1.000000e+11 big number,-10.00 float,xxxxxxxxxxand so on!
【特别注意】
1.arg1、arg2、arg++ 参数将被插入主字符串中的百分号(%)符号处。该函数是逐步执行的。在第一个 % 符号处,插入 arg1,在第二个 % 符号处,插入 arg2,依此类推。 2.如果 % 符号多于 arg 参数,则您必须使用占位符。占位符被插入到 % 符号后面,由数字和 "\$" 组成。 3.PHP 版本:4+。
【原版定义】
/** * Return a formatted string * @link http://www.php.net/manual/en/function.sprintf.php * @param format string <p> * 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. This applies to both sprintf * and printf. * </p> * <p> * Each conversion specification consists of a percent sign * (%), followed by one or more of these * elements, in order: * An optional sign specifier that forces a sign * (- or +) to be used on a number. By default, only the - sign is used * on a number if it's negative. This specifier forces positive numbers * to have the + sign attached as well, and was added in PHP 4.3.0. * @param args mixed[optional] <p> * </p> * @param _ mixed[optional] * @return string a string produced according to the formatting string * format. */
转载请注明出处:php1234.cn ,原文地址:http://www.php1234.cn/a/functions/2016/1110/127.html
收藏文章

登录
表情删除后不可恢复,是否删除
取消
确定
图片正在上传,请稍后...
取消上传
评论内容为空!
还没有评论,快来抢沙发吧!
畅言云评
站长点击我去广告>
热评话题
- 德者,人之所得,使万物各得其所欲_PHP1234|PHP修炼之路|跟着明礼馨德一起修炼PHP
- 义者,人之所宜,赏善罚恶,以立功立事_PHP1234|PHP修炼之路|跟着明礼馨德一起修炼PHP
- 夫欲为人之本,不可无一焉_PHP1234|PHP修炼之路|跟着明礼馨德一起修炼PHP
- 潜居抱道,以待其时!_PHP1234|PHP修炼之路|跟着明礼馨德一起修炼PHP
- 极人臣之位;成绝代之功;如其不遇,没身而已_PHP1234|PHP修炼之路|跟着明礼馨德一起修炼PHP
- 明于盛衰之道,通乎成败之数;审乎治乱之事,_PHP1234|PHP修炼之路|跟着明礼馨德一起修炼PHP
- 仁者,人之所亲,有慈惠恻隐之心,以遂其生成_PHP1234|PHP修炼之路|跟着明礼馨德一起修炼PHP