Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2020-07-22 05:04:08

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,007
Website GitHub Mastodon Twitter

[SOLVED] trim and then replace

Is there a way to trim the last 3 digits of a number, ie 11516798 and then replace them with zeros, ie 11516000?

> Edited to add. I’m trying this in a variable.

Last edited by colak (2020-07-22 05:06:42)


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#2 2020-07-22 08:07:56

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,595
GitHub Twitter

Re: [SOLVED] trim and then replace

Maybe with the help of a Regex. See here: https://regex101.com/r/HhEwlJ/1

[Updated] Simplier (https://regex101.com/r/HhEwlJ/2):

'/([^0+])/'

Last edited by Pat64 (2020-07-22 08:12:36)


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#3 2020-07-22 08:32:16

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

Re: [SOLVED] trim and then replace

You should be able to do this using the Xpath substring and string-length commands (with concat) in txp:evaluate.

<!-- original number in a variable -->
<txp:variable name="id_num">11516798</txp:variable>
<!-- strip last three chars from string and add "000" to end with XPath -->
<txp:evaluate query='concat(substring("<txp:variable name="id_num" />", 1, string-length("<txp:variable name="id_num" />") - 3), "000")' />

Or if your input number is in a custom field and you want to output a variable you might do it like this:

<!-- original number in custom field: 'id_num' -->
<!-- strip last three chars from string and add "000" to end using XPath and store in variable -->
<txp:variable name="id_anonymised" escape="trim">
    <txp:evaluate query='concat(substring("<txp:custom_field name="id_num" />", 1, string-length("<txp: custom_field name="id_num" />") - 3), "000")' />
</txp:variable>

<!-- show variable -->
<txp:variable name="id_anonymised" />

A bit convoluted compared to using php, but you can use that without having to add php tags to the evaluate tag’s advanced options.


TXP Builders – finely-crafted code, design and txp

Offline

#4 2020-07-22 08:45:07

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

Re: [SOLVED] trim and then replace

Pat64 wrote #324756:

Maybe with the help of a Regex. See here: https://regex101.com/r/HhEwlJ/1

[Updated] Simplier (https://regex101.com/r/HhEwlJ/2):

'/([^0+])/'

Interesting. I would have thought Yiannis wants:

(.*)\w{3} -> $1000

e.g.: your regex101 link v3.

But how can we use that in txp:evaluate? I looked at XPath matches and replace but get an DOMXPath::evaluate(): Unregistered function error. I guess those are not in XPath 1.0.


TXP Builders – finely-crafted code, design and txp

Offline

#5 2020-07-22 08:55:09

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,007
Website GitHub Mastodon Twitter

Re: [SOLVED] trim and then replace

Hi Pete, thanks! Regex is relly confusing to me. This is what I am trying in a form.

<txp:variable name="trimstring" value='<txp:yield name="string" />' trim="([^0+])" />
<txp:if_yield name="string">
<txp:variable name="trimstring" />
<txp:if_yield>

and the shortcode in the article: <txp::trim string="10987654321" /> from which I would like to get 10987654000.

I could of course not worry about the replacement and just have

<txp:variable name="trimstring" value='<txp:yield name="string" />' trim="([^0+])" />
<txp:if_yield name="string">
<txp:variable name="trimstring" />000
<txp:if_yield>

In any case, the regex did not trim anything:(


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#6 2020-07-22 09:06:34

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,595
GitHub Twitter

Re: [SOLVED] trim and then replace

Hi Julian ;)

Your Regex can be changed by this one https://regex101.com/r/HhEwlJ/4

@Colak. Is that what your’re looking for (not tested)?

<txp:custom_field name="id_num" trim='/(\d{5})(\d+)/' replace='$1000' />

Last edited by Pat64 (2020-07-22 09:08:39)


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#7 2020-07-22 09:17:03

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

Re: [SOLVED] trim and then replace

How have I never seen the trim and replace combo? That does make using regex a lot easier.

Pat64 wrote #324761:

Your Regex can be changed by this one: https://regex101.com/r/HhEwlJ/4

That works too but keeps the first 5 chars and discards everything after it so it assumes a fixed character length. The version above should discard only the last 3 chars regardless of how long the number is, but you’re right that it should use \d.

Interestingly $1000 doesn’t work (presumably it expects the 1000th match group) but you can append the 000 using wraptag to avoid getting output if the tag produces nothing.

EDIT: This should do what you want with regex.

<txp:variable name="id_num">10987654321</txp:variable>
<txp:variable name="id_num" trim="/(.*)\d{3}/" replace="$1" wraptag="<+>000" />

or in the case of your shortcode:

<txp:variable name="trimstring" escape="trim">
    <txp:yield name="string" trim="/(.*)\d{3}/" replace="$1" wraptag="<+>000" />
</txp:variable>

or drop the variable wrapper entirely if you just want the output back.

<txp:yield name="string" trim="/(.*)\d{3}/" replace="$1" wraptag="<+>000" />

Assuming that the trim/replace combo works for txp:yield, it should output nothing if no string attribute is passed via the shortcode tag.


TXP Builders – finely-crafted code, design and txp

Offline

#8 2020-07-22 09:48:54

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,007
Website GitHub Mastodon Twitter

Re: [SOLVED] trim and then replace

Hey Julian, I was tying when you were posting and didn’t notice your posts. It works!

Final code:

<txp:if_yield name="string">
<txp:variable name="id_num" value='<txp:yield name="string" />' />
<txp:variable name="id_num" trim="/(.*)\d{3}/" replace="$1" wraptag="<+>000" />
</txp:if_yield>

Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#9 2020-07-22 20:19:46

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

Re: [SOLVED] trim and then replace

jakob wrote #324762:

Interestingly $1000 doesn’t work (presumably it expects the 1000th match group)

For the record, this should work:

<txp:variable name="id_num" trim="/(.*)\d{3}/" replace="${1}000" />

Offline

#10 2020-07-23 03:46:36

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,007
Website GitHub Mastodon Twitter

Re: [SOLVED] trim and then replace

etc wrote #324784:

For the record, this should work:

<txp:variable name="id_num" trim="/(.*)\d{3}/" replace="${1}000" />...

^^ Indeed. Thanks!


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#11 2020-07-23 08:02:29

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

Re: [SOLVED] trim and then replace

etc wrote #324784:

For the record, this should work:

<txp:variable name="id_num" trim="/(.*)\d{3}/" replace="${1}000" />...

Thank you, that’s good to know, especially as the <+> in the wraptag="<+>000" method can cause unexpected difficulties if not notextiled with ==<tag... />== when used mid-line in the body field as I discovered when testing.


TXP Builders – finely-crafted code, design and txp

Offline

#12 2020-07-24 04:34:12

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,595
GitHub Twitter

Re: [SOLVED] trim and then replace

etc wrote #324784:

For the record, this should work:

<txp:variable name="id_num" trim="/(.*)\d{3}/" replace="${1}000" />...

I’m confused Oleg: what the raison why you don’t use this for your Regex (only filter digits)?

/(\d+)\d{3}/

Last edited by Pat64 (2020-07-24 04:34:47)


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

Board footer

Powered by FluxBB