


Analysis of the operating principle of PHP vsprintf() function formatted string
实例
把格式化字符串写入变量中:
<?php $number = 9; $str = "Beijing"; $txt = vsprintf("There are %u million bicycles in %s.",array($number,$str)); echo $txt; ?>
定义和用法
vsprintf()
函数把格式化字符串写入变量中。
与 sprintf() 不同,vsprintf() 中的参数位于数组中。数组元素将被插入到主字符串中的百分号(%)符号处。该函数是逐步执行的。在第一个 % 符号处,插入第一个数组元素,在第二个 % 符号处,插入第二个数组元素,依此类推。
注释:如果 % 符号多于arg参数,则您必须使用占位符。占位符被插入到 % 符号之后,由数字和 "\$" 组成。请参见例子 2。
语法
vsprintf(format,argarray)
参数 | 描述 |
---|---|
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\](规定小数位数或最大字符串长度)注释:如果使用多个上述的格式值,它们必须按照上面的顺序进行使用,不能打乱。 |
argarray | 必需。带有参数的一个数组,这些参数会被插到 format 字符串中的 % 符号处。 |
技术细节
返回值:以格式化字符串的形式返回数组值。PHP 版本:4.1.0+
更多实例
例子 1
使用格式值 %f:
<?php $num1 = 123; $num2 = 456; $txt = vsprintf("%f%f",array($num1,$num2)); echo $txt; ?>
例子 2
使用占位符:
<?php $number = 123; $txt = vsprintf("有两位小数:%1\$.2f<br>没有小数:%1\$u",array($number)); echo $txt; ?>
例子 3
使用 sprintf() 来演示所有可能的格式值:
<?php $num1 = 123456789; $num2 = -123456789; $char = 50; // ASCII 字符 50 是 2 // 注释:格式值 "%%" 返回百分号 echo sprintf("%%b = %b",$num1)."<br>"; // 二进制数 echo sprintf("%%c = %c",$char)."<br>"; // ASCII 字符 echo sprintf("%%d = %d",$num1)."<br>"; // 带符号的十进制数 echo sprintf("%%d = %d",$num2)."<br>"; // 带符号的十进制数 echo sprintf("%%e = %e",$num1)."<br>"; // 科学计数法(小写) echo sprintf("%%E = %E",$num1)."<br>"; // 科学计数法(大写) echo sprintf("%%u = %u",$num1)."<br>"; // 不带符号的十进制数(正) echo sprintf("%%u = %u",$num2)."<br>"; // 不带符号的十进制数(负) echo sprintf("%%f = %f",$num1)."<br>"; // 浮点数(视本地设置) echo sprintf("%%F = %F",$num1)."<br>"; // 浮点数(不视本地设置) echo sprintf("%%g = %g",$num1)."<br>"; // 短于 %e 和 %f echo sprintf("%%G = %G",$num1)."<br>"; // 短于 %E 和 %f echo sprintf("%%o = %o",$num1)."<br>"; // 八进制数 echo sprintf("%%s = %s",$num1)."<br>"; // 字符串 echo sprintf("%%x = %x",$num1)."<br>"; // 十六进制数(小写) echo sprintf("%%X = %X",$num1)."<br>"; // 十六进制数(大写) echo sprintf("%%+d = %+d",$num1)."<br>"; // 符号说明符(正) echo sprintf("%%+d = %+d",$num2)."<br>"; // 符号说明符(负) ?>
例子 4
字符串说明符的演示:
<?php $str1 = "Hello"; $str2 = "Hello world!"; echo vsprintf("[%s]",array($str1))."<br>"; echo vsprintf("[%8s]",array($str1))."<br>"; echo vsprintf("[%-8s]",array($str1))."<br>"; echo vsprintf("[%08s]",array($str1))."<br>"; echo vsprintf("[%'*8s]",array($str1))."<br>"; echo vsprintf("[%8.8s]",array($str2))."<br>"; ?>
相关学习推荐:PHP编程从入门到精通
The above is the detailed content of Analysis of the operating principle of PHP vsprintf() function formatted string. For more information, please follow other related articles on the PHP Chinese website!

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
