In PHP, the sprintf function can write the formatted string into a variable for escape. The syntax is "sprintf(format, which stipulates that it is inserted into the first % symbol in the format string. Parameters, specify the parameters inserted at the second % symbol in the format string,...)", where format is used to specify the string and how to format the variables in it.
The operating environment of this article: Windows 10 system, PHP version 8.1, Dell G3 computer
How to escape the sprintf function in php
sprintf() function writes a formatted string into a variable.
arg1, arg2, parameters will be inserted into the main string at the percent sign (%) symbol. This function is executed step by step. At the first % sign, insert arg1, at the second % sign, arg2, and so on.
Note: If there are more % symbols than arg arguments, you must use placeholders. The placeholder is inserted after the % symbol and consists of a number and "\$".
The syntax is:
sprintf(format,arg1,arg2,arg++)
format required. Specifies a string and how to format variables within it.
Possible format values:
- ##%% - Returns a percent sign %
- %b - Binary number
- %c - the character corresponding to the ASCII value
- %d - the decimal number containing the sign (negative, 0, positive)
- %e - Use lowercase scientific notation (e.g. 1.2e 2)
- %E - Use uppercase scientific notation (e.g. 1.2 E 2)
- %u - Decimal number without sign (greater than or equal to 0)
- %f - Floating point number (local settings)
- %F - Floating point number (non-native settings) ##%g - Shorter %e and %f
- %G - shorter %E and %f
- ##%o - octal number
- %s - String
- %x - Hexadecimal number (lowercase letters)
- %X - Hexadecimal number (uppercase letters)
- Additional format value. Must be placed between % and a letter (such as %.2f):
- (Add or - in front of the number to define the sign of the number. Default In this case, only negative numbers are marked, positive numbers are not marked)
- ' (Specifies what to use as padding, the default is spaces. It must be used with the width specifier. For example: %'x20s (use "x" as padding))
- - (left adjust variable value)
- [0-9] (specify The minimum width of the variable value)
- .[0-9] (Specifies the number of decimal places or the maximum string length)
- Note: If When using multiple above format values, they must be used in the order above and cannot be disrupted.
<?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>"; // 符号说明符(负) ?>Output result:
Recommended learning: "PHP Video Tutorial
The above is the detailed content of How to escape sprintf function in php. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools