PHP sprintf 转义 %
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3666734/
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
PHP sprintf escaping %
提问by Sandeepan Nath
I want the following output:-
我想要以下输出:-
About to deduct 50% of 27.59 from your Top-Up account.
即将从您的充值账户中扣除 27.59 的 50%。
when I do something like this:-
当我做这样的事情时:-
$variablesArray[0] = '';
$variablesArray[1] = 27.59;
$stringWithVariables = 'About to deduct 50% of %s %s from your Top-Up account.';
echo vsprintf($stringWithVariables, $variablesArray);
But it gives me this error vsprintf() [function.vsprintf]: Too few arguments in ...
because it considers the %
in 50%
also for replacement. How do I escape it?
但它给了我这个错误vsprintf() [function.vsprintf]: Too few arguments in ...
,因为它认为%
在50%
还进行更换。我该如何逃脱?
回答by BoltClock
Escape it with another %
:
用另一个来逃避它%
:
$stringWithVariables = 'About to deduct 50%% of %s %s from your Top-Up account.';
回答by Sandeepan Nath
It is very easy.
这很容易。
Put another %
in front of the original %
to escape it.
%
在原来的前面放另一个%
以逃避它。
For example,
例如,
$num=23;
printf("%%d of 23 = %d",$num);
Output:
输出:
%d of 23 = 23
回答by 3eighty
What about this:
那这个呢:
$variablesArray[0] = '%';
$variablesArray[1] = '';
$variablesArray[2] = 27.59;
$stringWithVariables = 'About to deduct 50%s of %s %s from your Top-Up account.';
echo vsprintf($stringWithVariables, $variablesArray);
Just add your percent sign in your variables array
只需在变量数组中添加百分号