Home  >  Article  >  Backend Development  >  How to use php sprintf function

How to use php sprintf function

青灯夜游
青灯夜游Original
2021-03-30 10:46:162148browse

sprintf()是PHP中的内置函数,可以用于把字符串进行多种类型的格式化,然后返回已格式化的字符串;在处理xml数据格式时,需要用到该函数来格式化。语法格式“sprintf(format,arg1,arg2,arg++)”。

How to use php sprintf function

本教程操作环境:windows7系统、PHP7.1版,DELL G3电脑

PHP sprintf() 函数

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

  • 用处:把字符串进行多种类型的格式化

  • 用于:处理xml数据格式时,需要用到他来格式化等等

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

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

语法:

sprintf(format,arg1,arg2,arg++)

How to use php sprintf function

返回值:返回已格式化的字符串。    

示例:

<?php
$str1="1234";
echo sprintf("hello%s","$str1");
//效果为: hello1234
?>

这什么意思呢

要点:

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

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

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

那么多个值怎么格式化一起呢?

看:

<?php
$a="abcdef";
$b="abcdef";
$c="1234";
echo sprintf("%1\$s%2\$s",$c,$a);
//效果为: 1234abcdef
?>

%s为一个标记,两个%s%s这样写却是错误的,每个%s必须标记键位,不然我怎么知道都代表格式化后面的哪个$str呢,所以有个特别的语法

%1\$%2\$      解释:%1表示格式化sprintf("%1\$%2\$",''$str1","$str2")中对应的$str1,那么%2自然表示格式化$str2,\$是代表有多个值准备格式化,所以每个%1和%2或者还有%3的标记后都要加这个符号代表一行中有多个标记,如果只有一个标记就不用\$了占位符了,记住$str2、$str3是可选,也就是可以不格式化这么多

讲个特殊的例子

<?php
$a="abcdef";
$b="abcdef";
$c="1234";
echo sprintf("%&#39;x13.2f",$c);
// 效果为:xxxxxx1234.00
//echo sprintf("%06.2f", $a);
?>

sprintf("%'x13.2f",$c);是什么意思,f是浮点数,第一步按照格式 %   '(补位值) 宽度值  格式化类型  这三部分,语法之间必须紧挨着不能用空格

必须解释一下何为补位值:就是设定的宽度超出了,用这个值来填上

解释一下,补位值只有设置宽度超出了目标值才能用

所以就是用x补位,13为总宽度,2为小数点后的宽度,f为格式化类型,别急我会解释

 ' 号(单引号)代表接下来要用补位类型

为什么他能识别x是补位值呢,因为前面有 ' 号,

为什么他能识别哪几位是哪种类型呢,他是这样识别的,按顺序从先从两头的字符开始识别是什么类型,补位值肯定是单数位,不可能一个位置补两个数吧,所以左边第一位x是补位值,右边第一位是格式化类型,然后中间的自然是宽度值

第二,为什么小数点 后还有一个属性 ,因为这是f(浮点型),必须设置小数点后有几位,不能小数后面不能设置了吧,那浮点数意义何在呢

不要觉得烦,每个例子都是浓缩出来的

整数补位:

<?php
$a="abcdef";
$b="abcdef";
$c="1234";
echo sprintf("%&#39;07s",$c);
//结果是:0001234
?>

这就是整数补位,还是一样
第一步按照格式 %   '(补位值) 宽度值  格式化类型  这三部分

0是补位值 7是宽度值 s自然是格式化类型

还有一种最重要的例子

<?php
$a="abcdef";
$b="abcdef";
$c="1234";
echo sprintf("[%-6s]",$c);       //结果是:[1234 ]
echo sprintf("[%-4s]",$c);       //结果是:[1234]
echo sprintf("[%4.2s]",$c);       //结果是:[ 12]
?>

这个第一步 [ ] 仅仅只是修饰,不用理解

第二步,没有 ' 号,证明没有补位,无需添加补位值

所以语法格式为 : %    宽度值  格式化类型  这两部分

第一二行解释如下:

第一个宽度为6,但是$c=1234 ,只有4个字符,所以宽度不够,所以右边自动扩充(扩充再多也只显示一个空格位置),为什么在右边扩充,因为宽度前有个 - 号,代表补位方向相反,如在补位值前加-,自然从右边开始补位

下面为什么没变化,因为宽度正好一致,只是补位的方向改变了

第三行解释如下:

Don't be fooled, the syntax structure is still the same % Width value Format type These two parts

There is no ' sign, which proves that there is no padding and there is no need to add padding value

So 4.2 is still the same It’s the width value

, but the 4 on the left of the decimal point represents the total width, and the 2 on the right represents that only 2 digits are displayed, so there are two more vacancies, so two vacancies are expanded from the left, why is only one space displayed in the previous paragraph? I said it, let’s say it again, no matter how much expansion is done, only one space will be displayed. By default, start from the left

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to use php sprintf function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn