1

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.

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 }} – stevecomrie Jul 13 '20 at 18:20
2

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

Final code:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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