197
votes

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?

5
@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
@sandeepan: vsprintf belongs in the same family of functions as printf. The correct documentation to find the format, though, is php.net/sprintf. Both pages even point to it: "See sprintf() for a description of format." Didn't you at least click it? - BoltClock♦
@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
oh I thought the second comment was by Col. Shrapnel , sorry - Sandeepan Nath
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

5 Answers

378
votes

Escape it with another %:

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

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
votes

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

0
votes

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

0
votes

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)