Escape de PHP sprintf %


Quiero la siguiente salida: -

A punto de deducir el 50% de € 27.59 de su cuenta de recarga.

Cuando hago algo como esto:-

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

Pero me da este error vsprintf() [function.vsprintf]: Too few arguments in ... porque considera el % en 50% también para el reemplazo. ¿Cómo puedo escapar?

Author: Rubens Mariuzzo, 2010-09-08

3 answers

Escapar con otro %:

$stringWithVariables = 'About to deduct 50%% of %s %s from your Top-Up account.';
 271
Author: BoltClock,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-08 10:26:43

Es muy fácil.

Pon otro % delante del original % para escapar de él.

Por ejemplo,

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

Salida:

%d of 23 = 23
 0
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-02 23:40:30

¿Qué hay de esto:

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

Simplemente agregue su signo de porcentaje en su matriz de variables

 0
Author: 3eighty,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-01 14:48:30