<p>I figured out that the best way to do this was to replace the newline character <code>\n</code>.</p>
<p>Final code:</p>
<pre><code>{{ plainText|replace({ &quot;\n&quot; : &quot;, &quot;}) }}
</code></pre>

CC BY-SA 4.0
2020-07-13T16:50:23.150Z
Line endings are unfortunately a good bit more involved than just `"\n"` -- if you can't control the input, check out https://stackoverflow.com/questions/7836632/how-to-replace-different-newline-styles-in-php-the-smartest-way
andrew.welch
CC BY-SA 4.0
2020-07-13T16:35:20.493Z
2020-07-13T16:35:20.493Z
Jake Dohm
<p>I need to convert a new line in a plain multiline text field in Craft into a comma.</p>
<p>My text looks like:</p>
<pre><code>1 Infinite Loop
Cupertino, CA
</code></pre>
<p>and I want to convert it to: <code>1 Infinite Loop, Cupertino, CA</code></p>
<p>I tried doing <code>{{ text|nl2br|replace({ '&lt;br&gt;', ', ' })|raw }}</code> but that didn't work.</p>

CC BY-SA 4.0
2020-07-13T18:20:55.823Z
I suspect nl2br was actually inserting <br/> or <br /> instead of <br>, so your replace wasn't matching anything.

This would probably do it: {{ text|nl2br|replace({ '<br>': ', ', '<br/>': ', ', '<br />': ', ' })|raw }}
stevecomrie
<templating>
How can I replace a newline (in a plain text field) with a comma?
# How can I replace a newline (in a plain text field) with a comma?

(Asked by: username_0 on 2020-07-13)

I need to convert a new line in a plain multiline text field in Craft into a comma.
My text looks like:
1 Infinite Loop
Cupertino, CA

and I want to convert it to: 1 Infinite Loop, Cupertino, CA
I tried doing {{ text|nl2br|replace({ '', ', ' })|raw }} but that didn't work.

**Tags:** templating

> **Comments:**
> **username_1**: I suspect nl2br was actually inserting <br/> or <br /> instead of <br>, so your replace wasn't matching anything.  This would probably do it: {{ text|nl2br|replace({ '<br>': ', ', '<br/>': ', ', '<br />': ', ' })|raw }}  

---

## 1 answers

### Answer by username_0 on 2020-07-13. Score: 2

I figured out that the best way to do this was to replace the newline character \n.
Final code:
{{ plainText|replace({ "\n" : ", "}) }}

> **Comments:**
> **username_2**: Line endings are unfortunately a good bit more involved than just `"\n"` -- if you can't control the input, check out https://stackoverflow.com/questions/7836632/how-to-replace-different-newline-styles-in-php-the-smartest-way  

---

