Home > Backend Development > PHP Tutorial > Detailed usage of sprintf function php, sprintf function php_PHP tutorial

Detailed usage of sprintf function php, sprintf function php_PHP tutorial

WBOY
Release: 2016-07-11 10:36:19
Original
1488 people have browsed it

sprintf函数php的详细使用方法,sprintf函数php

 

PHP sprintf() 函数

先说下为什么要写这个函数的前言,这个是我在微信二次开发的一个token验证文档也就是示例文档看到的一个函数,当时非常不理解,于是查了百度,但是很多结果都很笼统,结果也很少,后来查阅了3c和问了公司的一些人做了一些测试慢慢懂了一些,一下就是我对sprintf函数的个人见解

 

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

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

 

基本语法格式:sprintf("%格式化类型","$str1","$str2") ;先别急我会一个个慢慢说

先看一下类型参照表,也就是要转换成什么类型的格式

这是转换格式的类型参照表

 

先举个最简单的案例

<?<span>php
</span><span>$str1</span>="1234"<span>;

</span><span>echo</span> <span>sprintf</span>("hello%s","<span>$str1</span>"<span>);

</span><span>//</span><span>效果为: hello1234</span>


?>
Copy after login

这什么意思呢

要点:

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

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

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

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

<?<span>php
</span><span>$a</span>="abcdef"<span>;
</span><span>$b</span>="abcdef"<span>;

</span><span>$c</span>="1234"<span>;
</span><span>echo</span> <span>sprintf</span>("%1\$s%2\$s",<span>$c</span>,<span>$a</span><span>);

</span><span>//</span><span>效果为: 1234abcdef</span>


?>
Copy after login

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

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

讲个特殊的例子

<?<span>php
</span><span>$a</span>="abcdef"<span>;
</span><span>$b</span>="abcdef"<span>;

</span><span>$c</span>="1234"<span>;
</span><span>echo</span> <span>sprintf</span>("%'x13.2f",<span>$c</span><span>);

</span><span>//</span><span> 效果为:xxxxxx1234.00


//echo sprintf("%06.2f", $a);</span>


?>
Copy after login

sprintf("%'x13.2f",$c);

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

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

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

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

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

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

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

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

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

整数补位

<?<span>php
</span><span>$a</span>="abcdef"<span>;
</span><span>$b</span>="abcdef"<span>;

</span><span>$c</span>="1234"<span>;
</span><span>echo</span> <span>sprintf</span>("%'07s",<span>$c</span><span>);

</span><span>//</span><span>结果是:0001234</span>


?>
Copy after login

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

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

还有一种最重要的例子

<?<span>php
</span><span>$a</span>="abcdef"<span>;
</span><span>$b</span>="abcdef"<span>;

</span><span>$c</span>="1234"<span>;
</span><span>echo</span> <span>sprintf</span>("[%-6s]",<span>$c</span>);       <span>//</span><span>结果是:[1234 ]</span>

<span>echo</span> <span>sprintf</span>("[%-4s]",<span>$c</span>);       <span>//</span><span>结果是:[1234]</span>

<span>echo</span> <span>sprintf</span>("[%4.2s]",<span>$c</span>);       <span>//</span><span>结果是:[ 12]</span>

?>
Copy after login

 

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

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

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

 

The first and second lines are explained as follows:

The first width is 6, but $c=1234, which is only 4 characters, so the width is not enough, so the right side is automatically expanded (no matter how much expansion is done, only one space will be displayed), why is it expanded on the right? Because There is a - sign before the width, which means the filling direction is opposite. If you add - before the filling value, the filling will naturally start from the right

Why is there no change below? Because the width is exactly the same, but the direction of filling has changed

The third line is explained as follows:

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

So 4.2 is still the width value

It’s just that 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 on the left. Why only one space is displayed? I said it in the previous paragraph, let’s say it again No matter how much you expand, only one space will be displayed. Start from the left by default

I believe it has been explained thoroughly. There is no place to condense it. If you don’t understand anything, you can leave a message. I am always here to help you solve it. Please extend your hand and give me a like. Thank you

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1140785.htmlTechArticleDetailed usage of sprintf function php, sprintf function php PHP sprintf() function Let’s first talk about why we need to write this function Foreword, this is a token verification article I developed on WeChat...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template