Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP sprintf转义%_Php_Escaping_Printf - Fatal编程技术网

PHP sprintf转义%

PHP sprintf转义%,php,escaping,printf,Php,Escaping,Printf,我想要以下输出:- 即将从您的充值账户中扣除27.59欧元的50% 当我这样做的时候:- $variablesArray[0] = '€'; $variablesArray[1] = 27.59; $stringWithVariables = 'About to deduct 50% of %s %s from your Top-Up account.'; echo vsprintf($stringWithVariables, $variablesArray); 但是它给了我一个错误vspri

我想要以下输出:-

即将从您的充值账户中扣除27.59欧元的50%

当我这样做的时候:-

$variablesArray[0] = '€';
$variablesArray[1] = 27.59;
$stringWithVariables = 'About to deduct 50% of %s %s from your Top-Up account.';
echo vsprintf($stringWithVariables, $variablesArray);
但是它给了我一个错误vsprintf[function.vsprintf]:在。。。因为它考虑50%中的%也用于替换。我如何逃出它?

用另一个%逃出它:

这很容易

将另一个%放在原始%前面,以将其转义

比如说,

$num=23;
printf("%%d of 23 = %d",$num);
输出:

%d of 23 = 23
那么这个呢:

$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);
只需在变量数组中添加百分号即可

这对我很有用:

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

// Gold (Cash Discount: %25, Deferred Discount: %20)

对于语言字符串中的add%,只需添加两倍的%%,而不是一个

@Col.shrapanel。我的问题是关于vsprintf而不是printf,我是第一次使用它,不能假设两者之间有相似之处。但是,在php.net/printf和php.net/vsprintf中搜索escape或escape都不会立即显示答案。当我搜索%%时,它会在php.net/printf中显示答案,但我不知道%%!!!你在投票表决前在那里搜索过答案吗?@sandepan:vsprintf和printf属于同一个函数族。但是,找到格式的正确文档是。这两页甚至都指向它:有关格式的描述,请参见sprintf。你没有至少点击它吗?@Col.Shrapnel好的,让我们看看php.net/sprintf,答案在哪里?使用printf和sprintf函数,转义字符不是反斜杠“\”,而是“%”。这里有什么可投票的?这对我来说并不像对你那么明显。如果你发现一个重复的问题,你可以更好地写链接。但我相信很多人会发现这个问题很有帮助。但是你不会接受的,你还是会说些什么,我知道。哦,我想第二句话是由斯帕奈尔上校说的,对不起,应该有RTFM回应的标志。这几乎就像人们玩巨魔一样,只是为了告诉人们阅读文档。他需要帮助,问了一个问题,然后有人帮他回答,并因此得到了分数。世界继续前进,互联网被用来为某人造福。与此同时,一场长达两年的争论让我火冒三丈。。。其中名称“%%s%%%s%%”,$fname,$lname;-丑陋,但它的工作!您可以将该部分添加到另一个%s:sprintf'从%s中您可以获得%s','something','50%'如果字符串是动态的,如何转义它?比如说,sprintf'这是%s'_title@madastrostr_替换“%”、“%”,标题问题是如何sprint转义%。
sprintf(
    '%s (Cash Discount: %%%s, Deferred Discount: %%%s)',
    $segment->name,
    $segment->discount_cash,
    $segment->discount_deferred,
)

// Gold (Cash Discount: %25, Deferred Discount: %20)