


Detailed usage of sprintf function php, sprintf function php_PHP tutorial
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> ?>
这什么意思呢
要点:
%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> ?>
%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> ?>
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> ?>
这就是整数补位,还是一样
第一步按照格式 % '(补位值) 宽度值 格式化类型 这三部分
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> ?>
这个第一步 [ ] 仅仅只是修饰,不用理解
第二步,没有 ' 号,证明没有补位,无需添加补位值
所以语法格式为 : % 宽度值 格式化类型 这两部分
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.
