It is very easy. Put another % in front of the original % to escape it.
PHP String sprintf() function. The sprintf() is an in-built function of PHP which writes a formatted string to a variable. It returns a formatted string.
In PHP, an escape sequence starts with a backslash \ . Escape sequences apply to double-quoted strings. A single-quoted string only uses the escape sequences for a single quote or a backslash.
Escape it with another %
:
$stringWithVariables = 'About to deduct 50%% of %s %s from your Top-Up account.';
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
For add %
in your language string, you just need to add double percent %%
instead of one
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With