PHP sprintf escaping %

后端 未结 3 974
無奈伤痛
無奈伤痛 2020-11-30 20:06

I want the following output:-

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

when I do something like this:-



        
相关标签:
3条回答
  • 2020-11-30 20:42

    Escape it with another %:

    $stringWithVariables = 'About to deduct 50%% of %s %s from your Top-Up account.';
    
    0 讨论(0)
  • 2020-11-30 20:49

    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 讨论(0)
  • 2020-11-30 20:53

    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
    
    0 讨论(0)
提交回复
热议问题