TransWikia.com

How can I replace a newline (in a plain text field) with a comma?

Craft CMS Asked by Jake Dohm on September 4, 2021

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.

One Answer

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

Final code:

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

Answered by Jake Dohm on September 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP