【问题标题】:How do I escape a new line character in a .ini file so that Zend_Config_Ini reads it literally?如何在 .ini 文件中转义换行符,以便 Zend_Config_Ini 逐字读取?
【发布时间】:2011-01-29 14:54:32
【问题描述】:

我正在尝试使用 PHP/Zend 框架将多行电子邮件存储在 ini 文件中。我的字符串中有换行符,当我使用 Zend_Config_Ini 解析 ini 文件时,换行符会转义,因此它们会打印在屏幕上,而不是换行符。

例子:

// ini file
message = Hi {0},\n\nThis is a test message.\nGoodbye!

被 Zend_Config_Ini 解析为:

Hi {0},\\n\\nThis is a test message.\\nGoodbye!

然后在电子邮件中打印为:

嗨,John,\n\n这是一条测试消息。\n再见!

我希望电子邮件看起来像这样:

嗨,约翰,

这是一条测试消息。
再见!

有人知道如何实现吗?谢谢!

【问题讨论】:

  • 您好,您是否在 ini 中找到了除使用双引号之外的另一种方法吗?

标签: php zend-framework newline ini


【解决方案1】:

如何使用双引号将您的值括起来,并使用真正的换行符,如下所示:

message = "Hi {0},

This is a test message.
Goodbye!"


例如,使用这部分代码:

$config = new Zend_Config_Ini(APPLICATION_PATH . '/config/application.ini');
var_dump($config->production->testmessage);
die;

application.ini 包含以下内容:

[production]

testmessage = "this is
a new
message"

我从var_dump 得到以下输出:

string(21) "this is
a new
message"

【讨论】:

    【解决方案2】:

    使用 php 常量:PHP_EOL

    message = "Hi {0}," PHP_EOL PHP_EOL "This is a test message." PHP_EOL "Goodbye!"
    

    请注意,application.ini 中的常量 PHP_EOL 必须出现在引号之外。否则它将被解释为文字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      相关资源
      最近更新 更多