Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-13T16:50:23.150Z
Answers.Text: 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
Answers.UserDisplayName: andrew.welch
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-13T16:35:20.493Z
Answers.LastActivityDate: 2020-07-13T16:35:20.493Z
Answers.UserDisplayName: Jake Dohm
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-07-13T18:20:55.823Z
Comments.Text: 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 }}
Comments.UserDisplayName: stevecomrie
Tags: <templating>
Title: How can I replace a newline (in a plain text field) with a comma?
ThreadText: # 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({ '<br>', ', ' })|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  
  
  ---
  
  