PHP sprintf escaping %

Solution 1:

Escape it with another %:

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

Solution 2:

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

Solution 3:

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

Solution 4:

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)