Php 向.ini文件值添加变量

Php 向.ini文件值添加变量,php,string,ini,Php,String,Ini,我正在测试一种通过每种语言的.ini文件运行翻译的方法。可以在这里找到测试站点,网址:www.exodus-squad.com 但是,在右侧框中,我希望显示以下文本: 随着减排的步伐不断加快, 电力和混合动力技术正在向中心舞台靠拢。在里面 2015年,发动机博览会将再次举办电动和混合动力汽车展览会 亭子 但是短语2015和Engine Expo都是PHP配置文件中的变量。当前,my.ini文件中的部分如下所示: [pavilion] texta = "With the drive to red

我正在测试一种通过每种语言的.ini文件运行翻译的方法。可以在这里找到测试站点,网址:www.exodus-squad.com

但是,在右侧框中,我希望显示以下文本:

随着减排的步伐不断加快, 电力和混合动力技术正在向中心舞台靠拢。在里面 2015年,发动机博览会将再次举办电动和混合动力汽车展览会 亭子

但是短语
2015
Engine Expo
都是PHP配置文件中的变量。当前,my.ini文件中的部分如下所示:

[pavilion]
texta  = "With the drive to reduce emissions continuing to gather pace, electric and hybrid technologies are moving closer to centre stage. In "
textb  = ", "
textc  = " will once again host the Electric & Hybrid Pavilion!"
<p>
    <?=$i['pavilion']['texta'];?>
    <?=$year?>
    <?=$i['pavilion']['textb'];?>
    <?=$show?>
    <?=$i['pavilion']['textc'];?>
</p>
texta  = "With the drive to reduce emissions continuing to gather pace, electric and hybrid technologies are moving closer to centre stage. In " . $year . ", " . $show . " will once again host the Electric &amp; Hybrid Pavilion!"
我的页面代码如下所示:

[pavilion]
texta  = "With the drive to reduce emissions continuing to gather pace, electric and hybrid technologies are moving closer to centre stage. In "
textb  = ", "
textc  = " will once again host the Electric &amp; Hybrid Pavilion!"
<p>
    <?=$i['pavilion']['texta'];?>
    <?=$year?>
    <?=$i['pavilion']['textb'];?>
    <?=$show?>
    <?=$i['pavilion']['textc'];?>
</p>
texta  = "With the drive to reduce emissions continuing to gather pace, electric and hybrid technologies are moving closer to centre stage. In " . $year . ", " . $show . " will once again host the Electric &amp; Hybrid Pavilion!"
或:

但两者都不是有效的语法。有人知道这是否可能吗


编辑

在收到两个答案后,my.ini文件现在看起来是这样的:

texta  = "With the drive to reduce emissions continuing to gather pace, electric and hybrid technologies are moving closer to centre stage. In %d, %s will once again host the Electric &amp; Hybrid Pavilion!"
<?=sprintf($i['pavilion']['texta'], $year, $show); ?>
我的代码如下所示:

texta  = "With the drive to reduce emissions continuing to gather pace, electric and hybrid technologies are moving closer to centre stage. In %d, %s will once again host the Electric &amp; Hybrid Pavilion!"
<?=sprintf($i['pavilion']['texta'], $year, $show); ?>
编辑#3

我甚至尝试过稍微分解代码,因此首先生成一个
$texta
变量,然后通过
sprintf()
将其回送到页面上,但输出仍然完全相同:

<?php
$texta = $i['pavilion']['texta'];
echo sprintf($texta, $year, $show);
?>


工作!谢谢大家。

您可以使用它,它可以让您在文本中添加占位符,以后可以填充。它可以做更多的事情,它真的很有用。。。您甚至可以使用带编号的占位符,这样就可以更改它们的顺序,这对于翻译来说非常方便

例如:

texta = "Blah Blah Blah %d Blah Blah %s"
然后在代码中:

<p>
    <?=sprintf($i['pavilion']['texta'], $year, $show)?>
</p>

  • 如果你对翻译感兴趣,你当然应该查一下

您可以使用字符串格式化程序:

text=“第一部分%s第二部分%s最后一部分”

然后在您的echo文件中:


请查看我的编辑。占位符按原样显示,
$show
$year
被连接到末尾。我自己做过测试,
通过
parse_ini_file()
创建的数组是否与手动创建的数组或变量有所不同,也许?@MatthewPeckham尝试手动创建的数组,但它不应该是不同的。请查看我的编辑。占位符按原样显示,
$show
$year
被连接到末尾。@MatthewPeckham这很奇怪:)。你确定括号没有放错位置吗?我尽可能肯定!我粘贴的是文件中的确切代码。@MatthewPeckham这里是一个例子。在那里很好用。您使用什么从ini文件中获取字符串?奇怪的是为什么它会在末尾添加变量<如果没有检测到占位符,代码>sprintf
通常会忽略它们。。。这就是为什么我怀疑一些错误的括号。我解析ini文件的代码已经添加到我的初始问题中。