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,395
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,680
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: 5,218
Website GitHub

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: 5,218
Website GitHub

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,395
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,680
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: 5,218
Website GitHub

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,395
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,689
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,395
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: 5,218
Website GitHub

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,680
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

#13 2020-07-24 08:00:04

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

Re: [SOLVED] trim and then replace

Pat64 wrote #324823:

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

You are right, Patrick, but the point is ${1}000 replacement pattern to append 000 to the first match $1, since $1000 does not work.

jakob wrote #324787:

<+> 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.

Amazing! Does <+> have a special meaning in Textile?

Offline

#14 2020-07-24 08:20:33

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,498
Website GitHub

Re: [SOLVED] trim and then replace

etc wrote #324825:

Does <+> have a special meaning in Textile?

No, but I expect if there’s a regex that matches tags (for escaping purposes?) then it might trip up.


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

Hire Txp Builders – finely-crafted code, design and Txp

Offline

#15 2020-07-26 16:29:20

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

Re: [SOLVED] trim and then replace

I’m asking this question here as it is directly related with the trim replace with an additional head-scratcher about how to use it in php.

Background: The reason for the trim/replace regex is because metacafe appears to be storing the stills of their videos in folders, allowing stills from up to 1000 videos. As you know my php is extremely limited but I have managed to make the code below work in an article

<txp:php>
$remoteFile = 'https://cdn.mcstatic.com/contents/videos_screenshots/12074000/12074819/830x467/2.jpg';
$handle = @fopen($remoteFile, 'r');
if(!$handle){
    echo 'File not found';
}else{
    echo '<img src="https://cdn.mcstatic.com/contents/videos_screenshots/12074000/12074819/830x467/2.jpg" width="830" height="467" alt="alt title" />';
}
</txp:php>

The php code is because metacafe users appear to have the option to delete stills but img numbers are augmented rather than replaced. I am now trying to include the above in the shortcode but testing it in an article using <txp::media from="mc" media="12100477" title="vid" />, yields no results.

<txp:hide>MetaCafe</txp:hide>
...
				<div class="gdpr">
					<txp:if_yield name="0">
					<txp:else />
						<txp:if_yield name="img">
							<txp:images id='<txp:yield name="img" />'><img src="<txp:site_url />images/<txp:yield name="img" /><txp:image_info type="ext" />" widh="<txp:image_info type="w" />" height="<txp:image_info type="h" />" alt="<txp:image_info type="alt" />" /></txp:images>
						<txp:else />
							<txp:variable name="id_num" value='<txp:yield name="media" />' />
							<txp:php>
								$remoteFile = 'https://cdn.mcstatic.com/contents/videos_screenshots/<txp:variable name="id_num" trim="/(.*)\d{3}/" replace="${1}000" />/<txp:yield name="media" />/830x467/2.jpg';
								$handle = @fopen($remoteFile, 'r');
								if(!$handle){
									echo 'no img';
								}else{
									echo '<img src="https://cdn.mcstatic.com/contents/videos_screenshots/<txp:variable name="id_num" trim="/(.*)\d{3}/" replace="${1}000" />/<txp:yield name="media" />/830x467/2.jpg" width="830" height="467" alt="<txp:yield name="title" />" />';
								}
							</txp:php>
						</txp:if_yield>
					</txp:if_yield>
...

I’m sure that the mistake is mine, unless of course I inadvertently hit on a bug.

ps. I am using
Textpattern version: 4.8.2-dev (abd4f0fc4a5b64fde6d62fe3c9aeb500)
on PHP version: 7.4.8

Last edited by colak (2020-07-26 16:32:14)


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

Offline

Board footer

Powered by FluxBB