Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I escape a new line character in a .ini file so that Zend_Config_Ini reads it literally?

I am trying to store a multiple line e-mail in an ini file using PHP/Zend Framework. My string has new lines characters in it, and when I use Zend_Config_Ini to parse the ini file, the new line characters come back escaped, so they are printed out on screen, instead of a line feed.

Example:

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

is parsed by Zend_Config_Ini as:

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

which then is printed out in the email as:

Hi John,\n\nThis is a test message.\nGoodbye!

Instead I want the e-mail to look like this:

Hi John,

This is a test message.
Goodbye!

Does anybody know how to achieve this? Thanks!

like image 659
Nick Avatar asked Mar 19 '10 19:03

Nick


People also ask

How to create a new INI file?

If you want to create a new INI file, you can create a .txt file first. Then, open this TXT file with Notepad, type command lines, click File > Save As, and then change the file name to XX.ini. In this way, you can create a new INI file. Of course, in the same way, you can convert an INI file into other file formats.

How to escape newline character from a string in Python?

Escape newline character from a string in Python. Let’s understand this with an easy example: Assume that you have to print a path or directory location of your local drive. The path is: Now if you use the below code to print it out as a string: Output: The n appears in that string and n creates a new line.

What is the INI file format for a test message?

// ini file message = Hi {0}, This is a test message. Goodbye! Hi {0},\ \ This is a test message.\ Goodbye! Hi John, This is a test message. Goodbye!


2 Answers

Use php constant: PHP_EOL

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

Note that the constant PHP_EOL in application.ini must appear outside of the quotation marks. Otherwise it'll be interpreted as a literal.

like image 167
falko Avatar answered Sep 30 '22 07:09

falko


What about using double-quotes arround your values, and using real newlines, like this :

message = "Hi {0},

This is a test message.
Goodbye!"


As an example, using this portion of code :

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

With application.ini containing this :

[production]

testmessage = "this is
a new
message"

I get the following output from my var_dump :

string(21) "this is
a new
message"
like image 37
Pascal MARTIN Avatar answered Sep 30 '22 08:09

Pascal MARTIN