How to escape sprintf function in php

WBOY
Release: 2023-03-16 12:08:02
Original
2422 people have browsed it

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.

How to escape sprintf function in php

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++)
Copy after login

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.
arg1 Required. Specifies the parameters to be inserted at the first % sign in the format string.

arg2 Optional. Specifies the parameter to be inserted into the format string at the second % sign.

arg Optional. Specifies the parameters to be inserted into the format string at the third, fourth, etc. % symbols.

The example is as follows:

<?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>"; // 符号说明符(负)
?>
Copy after login
Output result:

Recommended learning: "How to escape sprintf function in phpPHP 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!

Related labels:
php
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