Add string to text from Symfony2 Form Textarea

I have a form with Symfony 2 with a text box.

If the user writes something like this in the text box

word1

word2 word3 word4
word5

rest of message

I get the data exactly the same as from the form, but without any HTML lines. Therefore, if I want to use this text to send an email, it will be shown as

word1 word3 word4 word5 rest of message

How to save text formatting? Any suggestions?

+4
source share
2 answers

You can use nl2br :

$text = nl2br($text);

echo $text;
+5
source

If you want to use this in a Twig template, you should use a filter nl2brlike ..

{{ text|nl2br }} // NOT "bl2br" as I had originally written.
+8
source

All Articles