195

I want the following output:-

About to deduct 50% of € 27.59 from your Top-Up account.

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?

Rubens Mariuzzo
  • 25,735
  • 25
  • 111
  • 145
Sandeepan Nath
  • 8,771
  • 17
  • 66
  • 131
  • 1
    @Col. Shrapnel My question is about vsprintf not printf, I am using this for the first time and could not assume the similarity between the two. However, searching `escape` or `escaping` in both `php.net/printf` and `php.net/vsprintf` both does not show the answer immediately. When I search for `%%` it shows the answer in php.net/printf but I didn't know about `%%`!!! Did you search for the answer there before downvoting? – Sandeepan Nath Sep 08 '10 at 10:40
  • @sandeepan: `vsprintf` belongs in the same family of functions as `printf`. The correct documentation to find the format, though, is http://www.php.net/sprintf. Both pages even point to it: "See sprintf() for a description of format." Didn't you at least click it? – BoltClock Sep 08 '10 at 10:43
  • 5
    @Col. Shrapnel ok fine let's take php.net/sprintf, where is the answer? It is halfway down the page `With printf() and sprintf() functions, escape character is not backslash '\' but rather '%'.` What is there to downvote here? It was just not that obvious to me as it was to you. If you find a duplicate question you can better write the link. But I am sure many will find this question helpful. But you won't accept that and you will still say something, I know. – Sandeepan Nath Sep 08 '10 at 10:49
  • oh I thought the second comment was by Col. Shrapnel , sorry – Sandeepan Nath Sep 08 '10 at 10:51
  • 4
    SO should have a flag for RTFM responses. It's almost like people troll just so they can tell people to read the docs. He needed help and asked a question and then someone answered helpfully and got points for it. The world went on and the internet was used to someone's benefit. Meanwhile I'm getting heated over a two year old argument. – rob5408 Jul 02 '13 at 03:22

5 Answers5

374

Escape it with another %:

$stringWithVariables = 'About to deduct 50%% of %s %s from your Top-Up account.';
BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
5

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
1

For add % in your language string, you just need to add double percent %% instead of one

0

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

3eighty
  • 179
  • 2
  • 5
0

This works for me:

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

// Gold (Cash Discount: %25, Deferred Discount: %20)
Sinan Eldem
  • 4,712
  • 2
  • 32
  • 35