【问题标题】:Adding variables to an .ini file value将变量添加到 .ini 文件值
【发布时间】:2014-02-12 10:29:37
【问题描述】:

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

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

随着减排的步伐不断加快, 电动和混合动力技术正在接近中心舞台。在 2015年,Engine Expo将再次举办Electric & Hybrid 亭子!

但短语2015Engine Expo 都是PHP 配置文件中的变量。目前,我的 .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!"

或者:

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!"

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


编辑

收到几个答案后,我的 .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); ?>

但这只是在之后打印变量?

随着减少排放的努力继续加快步伐,电动和混合动力技术越来越接近中心舞台。在 %d,%s 将再次举办 Electric & Hybrid Pavilion!2015Engine Expo

编辑#2

这是我的parse_ini_file() 代码:

$find_lang = $_SERVER['REQUEST_URI'];
if (strpos($find_lang, '/fr/') !== false) {
    $lang = 'fr';
}
else if (strpos($find_lang, '/de/') !== false) {
    $lang = 'de';
}
else if (strpos($find_lang, '/it/') !== false) {
    $lang = 'it';
}
else {
    $lang = 'en';
}

$i = parse_ini_file($lang . ".ini", true);

编辑#3

我什至尝试过稍微分解代码,所以首先创建了一个$texta 变量,然后通过sprintf() 回显到页面上,但输出保持完全相同:

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

有效!谢谢大家。

【问题讨论】:

    标签: php string ini


    【解决方案1】:

    您可以使用sprintf,它可以让您在文本中放置占位符,以便稍后填写。而且它还可以做更多的事情,真的很有用...您甚至可以使用编号占位符,因此可以更改它们的顺序 - 非常方便翻译。

    例如:

    texta = "Blah Blah Blah %d Blah Blah %s"
    

    然后在你的代码中:

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

    【讨论】:

    • 请看我的编辑。占位符按原样显示,$show$year 连接到末尾。
    • @MatthewPeckham 这很奇怪:)。你确定你没有在错误的位置括号?
    • 尽我所能!我粘贴的是文件中的确切代码。
    • @MatthewPeckham 这是PHPFiddle。它在那里工作得很好。你用什么从ini文件中获取字符串?奇怪的是为什么它将变量添加到末尾...sprintf 如果没有检测到占位符,通常会简单地忽略它们...这就是为什么我怀疑一些行为不端的括号。
    • 我的解析ini文件的代码已添加到我最初的问题中。
    【解决方案2】:

    您可以使用字符串格式化程序:http://www.php.net/sprintf

    text="First part %s second part %s last part";

    然后在你的回显文件中:

    &lt;?php echo sprintf($i['pavilion']['text'], $year, $show); ?&gt;

    【讨论】:

    • 请看我的编辑。占位符按原样显示,$show$year 连接到末尾。
    • 我自己测试过,&lt;?php $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;amp; Hybrid Pavilion!"; echo sprintf($texta, 1000, 2000); 没有错。你能再检查一下吗?
    • 通过parse_ini_file() 创建的数组是否与手动创建的数组或变量有些不同?
    • @MatthewPeckham 尝试使用手动创建的数组,但应该不会有所不同。
    猜你喜欢
    • 2012-06-18
    • 2012-01-01
    • 2016-10-19
    • 1970-01-01
    • 2021-02-28
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多