登录  /  注册

php把格式化的字符串写入一个变量中函数sprintf()

黄舟
发布: 2017-11-03 09:48:00
原创
1918人浏览过

实例

把百分号(%)符号替换成一个作为参数进行传递的变量:

<?php
$number = 9;
$str = "Beijing";
$txt = sprintf("There are %u million bicycles in %s.",$number,$str);
echo $txt;
?>
登录后复制

定义和用法

sprintf() 函数把格式化的字符串写入一个变量中。

arg1、arg2、++ 参数将被插入到主字符串中的百分号(%)符号处。该函数是逐步执行的。在第一个 % 符号处,插入 arg1,在第二个 % 符号处,插入 arg2,依此类推。

注释:如果 % 符号多于 arg 参数,则您必须使用占位符。占位符被插入到 % 符号之后,由数字和 "\$" 组成。请参见实例 2。

提示:相关函数:printf()、 vprintf()、 vsprintf()、 fprintf() 和 vfprintf()

语法

sprintf(format,arg1,arg2,arg++)
登录后复制
参数描述
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] (规定小数位数或最大字符串长度)

注释:如果使用多个上述的格式值,它们必须按照上面的顺序进行使用,不能打乱。

arg1必需。规定插到 format 字符串中第一个 % 符号处的参数。
arg2可选。规定插到 format 字符串中第二个 % 符号处的参数。
arg++可选。规定插到 format 字符串中第三、四等等 % 符号处的参数。

技术细节

返回值:返回已格式化的字符串。
PHP 版本:4+

更多实例

实例 1

使用格式值 %f:

<?php
$number = 123;
$txt = sprintf("%f",$number);
echo $txt;
?>
登录后复制

实例 2

使用占位符:

<?php
$number = 123;
$txt = sprintf("With 2 decimals: %1$.2f
<br>With no decimals: %1$u",$number);
echo $txt;
?>
登录后复制

实例 3

所有可能的格式值的演示:

<?php
$num1 = 123456789;
$num2 = -123456789;
$char = 50; // The ASCII Character 50 is 2

// Note: The format value "%%" returns a percent sign
echo sprintf("%%b = %b",$num1)."<br>"; // Binary number
echo sprintf("%%c = %c",$char)."<br>"; // The ASCII Character
echo sprintf("%%d = %d",$num1)."<br>"; // Signed decimal number
echo sprintf("%%d = %d",$num2)."<br>"; // Signed decimal number
echo sprintf("%%e = %e",$num1)."<br>"; // Scientific notation (lowercase)
echo sprintf("%%E = %E",$num1)."<br>"; // Scientific notation (uppercase)
echo sprintf("%%u = %u",$num1)."<br>"; // Unsigned decimal number (positive)
echo sprintf("%%u = %u",$num2)."<br>"; // Unsigned decimal number (negative)
echo sprintf("%%f = %f",$num1)."<br>"; // Floating-point number (local settings aware)
echo sprintf("%%F = %F",$num1)."<br>"; // Floating-point number (not local sett aware)
echo sprintf("%%g = %g",$num1)."<br>"; // Shorter of %e and %f
echo sprintf("%%G = %G",$num1)."<br>"; // Shorter of %E and %f
echo sprintf("%%o = %o",$num1)."<br>"; // Octal number
echo sprintf("%%s = %s",$num1)."<br>"; // String
echo sprintf("%%x = %x",$num1)."<br>"; // Hexadecimal number (lowercase)
echo sprintf("%%X = %X",$num1)."<br>"; // Hexadecimal number (uppercase)
echo sprintf("%%+d = %+d",$num1)."<br>"; // Sign specifier (positive)
echo sprintf("%%+d = %+d",$num2)."<br>"; // Sign specifier (negative)
?>
登录后复制

先举个最简单的案例

<?php
$str1="1234";

echo sprintf("hello%s","$str1");

//效果为: hello1234


?>
登录后复制

这什么意思呢

要点:

%s = %符号和后面属性符号(s)总称为插入标记组合,也就是把后面准备进行格式化的值($str1)替换在这个位置 

hello = 这个单词就是很多人蒙蔽的地方,告诉你这个什么代表也没有,就单纯的代表一个hello,用于分割或者修饰用,一般用[ %s ]、这样格式化出来后就直接在标签里

记住,一个%标记符后面只有一个类型属性(比如s),s是什么上面有,以字符串的方式格式化

以上就是php把格式化的字符串写入一个变量中函数sprintf()的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号