PHP sprintf逃避%

San*_*ath 167 php printf escaping

我想要以下输出: -

即将从您的充值帐户中扣除50%的27.59欧元.

当我做这样的事情: -

$variablesArray[0] = '€';
$variablesArray[1] = 27.59;
$stringWithVariables = 'About to deduct 50% of %s %s from your Top-Up account.';
echo vsprintf($stringWithVariables, $variablesArray);
Run Code Online (Sandbox Code Playgroud)

但它给了我这个错误vsprintf() [function.vsprintf]: Too few arguments in ...,因为它认为%50%还进行更换.我怎么逃避它?

Bol*_*ock 317

逃避另一个%:

$stringWithVariables = 'About to deduct 50%% of %s %s from your Top-Up account.';
Run Code Online (Sandbox Code Playgroud)

  • sprintf("SELECT*FROM ... WHERE name LIKE'%%% s %%% s %%'",$ fname,$ lname); - 丑陋但它的作品! (21认同)
  • @madastro`str_replace('%','%%',the_title())` (7认同)

Moh*_*ran 8

要添加%语言字符串,您只需添加两个百分比%%而不是一个


小智 5

这很容易。

%在原来的前面放另一个%以逃避它。

例如,

$num=23;
printf("%%d of 23 = %d",$num);
Run Code Online (Sandbox Code Playgroud)

输出:

%d of 23 = 23
Run Code Online (Sandbox Code Playgroud)