breakLines not working as expected

Wintermute
Member | 13
+
0
-

Hello,

I use Latte 2.11. breakLines filter doesn't seem to work :

$question = 'Please answer the following math question.\nHow much is 7 + 3 ?';
<label for="antispam_answer">{$question|breakLines}</label>

The result is :

Please answer the following math question.\nHow much is 7 + 3 ?

Last edited by Wintermute (2024-05-02 07:40)

Rick Strafy
Nette Blogger | 78
+
+1
-

Hi, I have tried it and figured that \n needs to be a proper character, not a text. If you use single-quotes, then it's just a text, with double-quotes it's a unicode character (U+000A).

If you have some text stored somewhere, try replacing '\n' with "\n" before using breakLines.

Wintermute
Member | 13
+
0
-

Actually the string comes from a INI file and is handled with parse_ini_file and vsprintf.

nightfish
Member | 497
+
0
-

@Wintermute
The source of the string does not matter.
What matters is that the string does not contain character with ASCII code 10 (new line), but rather two separate characters with ASCII codes 92 (backslash) and 110 (lower case letter “n”). If you want to make breakLines work, you need to convert the string yourself: \str_replace('\n', "\n", $stringFromIniFile) should to the trick.

Function parse_ini_files() does not support standard escape sequences, as noted in the documentation: Note that the ini parser doesn't support standard escape sequences (\n, \t, etc.). If necessary, post-process the result of parse_ini_file() with stripcslashes() function.