我需要以下输出:-

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

当我这样做的时候:-

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

但它给了我这个错误vsprintf()[函数。vsprintf]:参数太少…因为它认为50%中的%也可以替换。我怎么逃脱呢?


当前回答

用另一个%转义它:

$stringWithVariables = 'About to deduct 50%% of %s %s from your Top-Up account.';

其他回答

用另一个%转义它:

$stringWithVariables = 'About to deduct 50%% of %s %s from your Top-Up account.';

这很简单。

在原来的%前面加上另一个%来转义它。

例如,

$num=23;
printf("%%d of 23 = %d",$num);

输出:

%d of 23 = 23

那么这个呢:

$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);

只要在变量数组中加上百分号

这对我来说很管用:

sprintf(
    '%s (Cash Discount: %%%s, Deferred Discount: %%%s)',
    $segment->name,
    $segment->discount_cash,
    $segment->discount_deferred,
)

// Gold (Cash Discount: %25, Deferred Discount: %20)

对于在你的语言字符串中添加%,你只需要添加两个%%而不是一个