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.

  • 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 }} Jul 13, 2020 at 18:20

1 Answer 1


I figured out that the best way to do this was to replace the newline character \n.

Final code:

{{ plainText|replace({ "\n" : ", "}) }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.