Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 Today 15:25:32

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,752
Website GitHub

How to get rid of newlines with tidy/trim

I have a list of column names like this, one item per line for readability:

ID,
Type,
SKU,
Name,
Short description,
Description,
Brands,
...

I want to return that as a simple comma-separated list without spaces where the newlines occur. Can I use trim/replace to do that? All my efforts so far have failed. I’ve tried wrapping it in:

<txp:evaluate escape="tidy, trim">
ID,
Type,
SKU,
Name,
Short description,
Description,
Brands
</txp:evaluate>
<txp:evaluate escape="tidy" trim="/,\s/gm" replace=",">
ID,
Type,
SKU,
Name,
Short description,
Description,
Brands
</txp:evaluate>
<txp:evaluate escape="tidy" trim=",
" replace=",">
ID,
Type,
SKU,
Name,
Short description,
Description,
Brands
</txp:evaluate>

And various other permutations (e.g. \r or \n). I always get back:

ID, Type, SKU, Name, Short description, Description, Brands,

with spaces between the terms. Any tricks? Running 4.9.0-dev.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#2 Today 15:53:47

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,900
Website GitHub

Re: How to get rid of newlines with tidy/trim

Try:

<txp:evaluate breakby="," break="," escape="tidy, trim">
ID,
Type,
SKU,
Name,
Short description,
Description,
Brands
</txp:evaluate>

TXP Builders – finely-crafted code, design and txp

Online

#3 Today 16:11:27

etc
Developer
Registered: 2010-11-11
Posts: 5,383
Website GitHub

Re: How to get rid of newlines with tidy/trim

Or this:

<txp:evaluate trim="/,\s+/s" replace="," />

Offline

#4 Today 16:35:22

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,752
Website GitHub

Re: How to get rid of newlines with tidy/trim

jakob wrote #339694:

Try: <txp:evaluate breakby="," break="," escape="tidy, trim">...

Winner! I didn’t try both break and breakby. No idea why both are needed but it works a treat, thank you.

etc wrote #339695:

<txp:evaluate trim="/,\s+/s" replace="," />...

Oooft, sooo close. It works but it also outputs an extra blank row beneath the headings in the resulting CSV file. My guess is escape="tidy" will fix that. Thank you. I wouldn’t have considered using /s as I foolishly went with /m since I thought, “Hey, it’s multi-line content…”

Edit: yep, adding escape="tidy" fixes that. Woot. Two solutions. You guys rock.

Last edited by Bloke (Today 16:40:19)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#5 Today 16:51:32

etc
Developer
Registered: 2010-11-11
Posts: 5,383
Website GitHub

Re: How to get rid of newlines with tidy/trim

Bloke wrote #339696:

Winner! I didn’t try both break and breakby. No idea why both are needed but it works a treat, thank you.

Because a lone break had broken some ancient (and actually meaningless) pattern. Actually, break always worked as ‘join’.

I wouldn’t have considered using /s as I foolishly went with /m since I thought, “Hey, it’s multi-line content…”

You probably don’t need a modifier at all here, it was a blind shot.

Offline

#6 Today 16:56:33

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,752
Website GitHub

Re: How to get rid of newlines with tidy/trim

etc wrote #339697:

Actually, break always worked as ‘join’.

Ah, yes of course. Naming conventions steeped in history…


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

Board footer

Powered by FluxBB