Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2025-01-27 12:46:30

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

trim + replace with multiple terms?

In my attempt to replace rah_repeat with a core tags, is there a way of doing trim and replace for multiple values at once?

My use case is changing the language of weekday codes:

<txp:evaluate trim="mo,tu,we,th,fr,sa,su" replace="mo,di,mi,do,fr,sa,su"><txp:variable name="weekday" /></txp:evaluate>

I have no luck with this and my guess is that it’s using the entire string rather than the individual values.


TXP Builders – finely-crafted code, design and txp

Offline

#2 2025-01-27 23:06:48

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

Re: trim + replace with multiple terms?

Nope, because presuming that commas always act as separator is restrictive. rah_repeat has a separator attribute (comma by default), but there is no global equivalent (break and breakby are already overcharged).

In your case, though, there is a weird core way:

<txp:evaluate query='substring("mo,di,mi,do,fr,sa,su", 1+string-length(substring-before("mo,tu,we,th,fr,sa,su", "<txp:variable name=''weekday'' />")), 2)' />

Offline

#3 2025-01-28 22:25:36

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

Re: trim + replace with multiple terms?

Hmm, clever. I did wonder if there was an XPath way but was looking at matches (which is not listed on MND as an xpath function?).*

If I’ve understood your method correctly, you’re getting the position of the matching weekday in the original text string, then using that character position to get the corresponding 2-letter segment of the replacement text string. I don’t think I would have thought of that.

Thanks for the suggestion, but for now I’ve stuck with rah_replace that still works well for this situation, not least because I have another situation (the corresponding JSON-LD part to schema.org opening times) where I’ve replaced with Monday, Tuesday, Wednesday, etc. and those terms don’t have a regular length.


TXP Builders – finely-crafted code, design and txp

Offline

#4 2025-01-28 22:53:59

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

Re: trim + replace with multiple terms?

jakob wrote #338949:

I did wonder if there was an XPath way but was looking at matches (which is not listed on MND as an xpath function?).*

Unfortunately, XPath 1.0 functions set (implemented in php) is quite small, but you know you can extend it via a pref.

Thanks for the suggestion, but for now I’ve stuck with rah_replace that still works well for this situation, not least because I have another situation (the corresponding JSON-LD part to schema.org opening times) where I’ve replaced with Monday, Tuesday, Wednesday, etc. and those terms don’t have a regular length.

Yep, it wouldn’t work here. But there is a small room for an Easter Egg: break attribute in, say,

<txp:evaluate break=",">th</txp:evaluate>

is useless (there is nothing to join). So we could consider that it applies to trim/replace:

<txp:evaluate trim="mo,tu,we,th,fr,sa,su" replace="mo,di,mi,do,fr,sa,su" break=",">th</txp:evaluate>

would replace ‘th’ with ‘do’ like rah_replace. Actually, str_replace() function used therein is a bit tricky: try

<txp:rah_replace from="a,b" to="b,a">ab</txp:rah_replace>

We might fix this too.

Offline

Board footer

Powered by FluxBB