%S 在 PHP、HTML 或 XML 中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11623224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does %S mean in PHP, HTML or XML?
提问by Wolfpack'08
I'm looking at Webmonkey's PHP and MySql Tutorial, Lesson 2. I think it's a php literal. What does %smean? It's inside the print_f()function in the while loops in at least the first couple of code blocks.
我正在查看Webmonkey 的 PHP 和 MySql 教程,第 2 课。我认为这是一个php文字。什么%s意思?它print_f()位于至少前几个代码块中的 while 循环中的函数内部。
printf("<tr><td>%s %s</td><td>%s</td></tr>n", ...
printf("<tr><td>%s %s</td><td>%s</td></tr>n", ...
回答by Tivie
with printf or sprintf characters preceded by the % sign are placeholders (or tokens). They will be replaced by a variable passed as an argument.
以 % 符号开头的 printf 或 sprintf 字符是占位符(或标记)。它们将被作为参数传递的变量替换。
Example:
例子:
$str1 = 'best';
$str2 = 'world';
$say = sprintf('Tivie is the %s in the %s!', $str1, $str2);
echo $say;
This will output:
这将输出:
Tivie is the best in the world!
蒂维是世界上最好的!
Note: There are more placeholders (%s for string, %d for dec number, etc...)
注意:有更多的占位符(%s 代表字符串,%d 代表十进制数字,等等...)
Order:
命令:
The order in which you pass the arguments counts. If you switch $str1 with $str2 as
您传递参数的顺序很重要。如果您将 $str1 与 $str2 切换为
$say = sprintf('Tivie is the %s in the %s!', $str2, $str1);
it will print
它会打印
"Tivie is the world in the best!"
“蒂维是世界上最好的!”
You can, however, change the reading order of arguments like this:
但是,您可以像这样更改参数的阅读顺序:
$say = sprintf('Tivie is the %2$s in the %1$s!', $str2, $str1);
which will print the sentence correctly.
这将正确打印句子。
Also, keep in mind that PHP is a dynamic language and does not require (or support) explicit type definition. That means it juggles variable types as needed. In sprint it means that if you pass a "string" as argument for a number placeholder (%d), that string will be converted to a number (int, float...) which can have strange results. Here's an example:
另外,请记住 PHP 是一种动态语言,不需要(或支持)显式类型定义。这意味着它会根据需要处理变量类型。在 sprint 中,这意味着如果您传递一个“字符串”作为数字占位符 (%d) 的参数,该字符串将被转换为一个数字 (int, float...),这可能会产生奇怪的结果。下面是一个例子:
$onevar = 2;
$anothervar = 'pocket';
$say = sprintf('I have %d chocolate(s) in my %d.', $onevar, $anothervar);
echo $say;
this will print
这将打印
I have 2 chocolate(s) in my 0.
我的 0 中有 2 个巧克力。
More reading at PHPdocs
更多阅读PHPdocs
回答by Ned Batchelder
In printf, %sis a placeholder for data that will be inserted into the string. The extra arguments to printfare the values to be inserted. They get associated with the placeholders positionally: the first placeholder gets the first value, the second the second value, and so on.
In printf,%s是将插入到字符串中的数据的占位符。的额外参数printf是要插入的值。它们在位置上与占位符相关联:第一个占位符获取第一个值,第二个占位符获取第二个值,依此类推。
回答by rdo
%sis a type specifier which will be replaced to valuable's value (string) in case of %s.
%s是一个类型说明符,在%s.
Besides %syou can use other specifiers, most popular are below:
除了%s您可以使用其他说明符之外,最流行的是以下:
d - the argument is treated as an integer, and presented as a (signed) decimal number.
d - 参数被视为整数,并表示为(有符号)十进制数。
f - the argument is treated as a float, and presented as a floating-point number (locale aware).
f - 参数被视为浮点数,并显示为浮点数(本地环境感知)。
s - the argument is treated as and presented as a string.
s - 参数被视为并显示为字符串。
回答by David
$num = 5;
$location = 'tree';
$format = 'There are %d monkeys in the %s';
echo sprintf($format, $num, $location);
Will output: "There are 5 monkeys in the tree."
将输出:“树上有 5 只猴子。”
回答by Emeka Augustine
The printf()or sprintf()function writes a formatted string to a variable.
Here is the Syntax:
该printf()或sprintf()功能的格式化字符串到变量。这是语法:
sprintf(format,arg1,arg2,arg++)
format:
格式:
- %% - Returns a percent sign
- %b - Binary number
- %c - The character according to the ASCII value
- %d - Signed decimal number (negative, zero or positive)
- %e - Scientific notation using a lowercase (e.g. 1.2e+2)
- %E - Scientific notation using a uppercase (e.g. 1.2E+2)
- %u - Unsigned decimal number (equal to or greater than zero)
- %f - Floating-point number (local settings aware)
- %F - Floating-point number (not local settings aware)
- %g - shorter of %e and %f
- %G - shorter of %E and %f
- %o - Octal number
- %s - String
- %x - Hexadecimal number (lowercase letters)
- %X - Hexadecimal number (uppercase letters)
- %% - 返回百分号
- %b - 二进制数
- %c - 根据 ASCII 值的字符
- %d - 有符号十进制数(负、零或正)
- %e - 使用小写的科学记数法(例如 1.2e+2)
- %E - 使用大写的科学记数法(例如 1.2E+2)
- %u - 无符号十进制数(等于或大于零)
- %f - 浮点数(本地设置感知)
- %F - 浮点数(不知道本地设置)
- %g - %e 和 %f 中的较短
- %G - %E 和 %f 中的较短
- %o - 八进制数
- %s - 字符串
- %x - 十六进制数(小写字母)
- %X - 十六进制数(大写字母)
arg1:
参数1:
- The argument to be inserted at the first %-signin the format string..(Required.)
- 要在格式字符串中的第一个% 符号处插入的参数..(必需。)
arg2:
参数2:
- The argument to be inserted at the second %-signin the format string. (Optional)
- 要在格式字符串中的第二个% 符号处插入的参数。(可选的)
arg++:
参数++:
- The argument to be inserted at the third, fourth, etc. %-signin the format string (Optional)
- 在第三被插入的参数,第四等%-sign格式字符串(可选)
Example 1:
示例 1:
$number = 9;
$str = "New York";
$txt = sprintf("There are approximately %u million people in %s.",$number,$str);
echo $txt;
This will output:
这将输出:
There are approximately
9million people inNew York.
大约有
9百万人在New York。
The arg1, arg2, arg++parameters will be inserted at percent (%) signs in the main string. This function works "step-by-step". At the first % sign, arg1 is inserted, at the second % sign, arg2 is inserted, etc.
的ARG1,ARG2,ARG ++参数将在主串以百分比(%)的标志插入。此功能“逐步”工作。在第一个 % 符号处插入 arg1,在第二个 % 符号处插入 arg2,以此类推。
Note: If there are more % signs than arguments, you must use placeholders. A placeholder is inserted after the % sign, and consists of the argument- number and "\$". Let see another Example:
注意:如果 % 符号多于参数,则必须使用占位符。占位符插入在 % 符号之后,由参数编号和“\$”组成。让我们看另一个例子:
Example 2
示例 2
$number = 123;
$txt = sprintf("With 2 decimals: %1$.2f
<br>With no decimals: %1$u",$number);
echo $txt;
This will output:
这将输出:
With 2 decimals:
123.00
With no decimals:123
有 2 位小数:
123.00
没有小数:123
Another important tip to remember is that:
要记住的另一个重要提示是:
With
printf()andsprintf()functions, escape character is not backslash '\' but rather '%'. Ie. to print '%' character you need to escape it with itself:
对于
printf()和sprintf()函数,转义字符不是反斜杠“\”而是“%”。IE。要打印 '%' 字符,您需要用它自己转义它:
printf('%%%s%%', 'Nigeria Naira');
printf('%%%s%%', 'Nigeria Naira');
This will output:
这将输出:
%Nigeria Naira%
%尼日利亚奈拉%
Feel free to explore the official PHP Documentation
随意浏览官方PHP 文档

