Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

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

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 3,892
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,397
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" />...

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: 3,768
Website

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: 9,637
Website

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.

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: 8,159
Website

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.org | hblack.net | LABS | State Machines | NeMe @ github | Covid-19; a resource
I do my best editing after I click on the submit button.

Offline

#16 2020-07-26 18:36:09

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 9,637
Website

Re: [SOLVED] trim and then replace

In the spirit of not repeating yourself, you might have more luck like this:

<txp:variable name="id_num"><txp:yield name="media" /></txp:variable>
<txp:variable name="remote_file">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</txp:variable>
<txp:php>
$handle = fopen(parse('<txp:variable name="remote_file" />'), 'r');
if ($handle) {
    echo '<img src="<txp:variable name="remote_file" />" width="830" height="467" alt="Testing" />';
}
</txp:php>

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

#17 2020-07-27 04:06:14

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 8,159
Website

Re: [SOLVED] trim and then replace

Thanks Stef, that works.

A last question. Is there a way to expand this in order to check for 3, 4, 5, 6, or 7.jpg if 2.jpg does not exist, and ignore the call if none of them exist?

> Edit: Just in case you missed it: forum.textpattern.com/viewtopic.php?pid=324836#p324836

> Edit 2

Unfortunately the suggestion breaks when there is no image in that url

Last edited by colak (2020-07-27 09:03:43)


Yiannis
——————————
neme.org | hblack.net | LABS | State Machines | NeMe @ github | Covid-19; a resource
I do my best editing after I click on the submit button.

Offline

#18 2020-07-27 09:46:38

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 8,159
Website

Re: [SOLVED] trim and then replace

So I am now using the original code I posted but with the variables as Stef suggested.

<txp:variable name="id_num"><txp:yield name="media" /></txp:variable>
<txp:variable name="remote_file">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</txp:variable>
<txp:php>
$remoteFile = '<txp:variable name="remote_file" />';
$handle = @fopen($remoteFile, 'r');
if(!$handle){
    echo 'Image not found';
}else{
    echo '<img src="<txp:variable name="remote_file" />" />';
}
</txp:php>
<txp:variable name="remote_file" /><txp:hide>temp: checking if the right url is parsed</txp:hide>

The problem is that I am still getting false negatives, ‘‘Image not found” even though the remote_file variable appears ok. Any ideas?


Yiannis
——————————
neme.org | hblack.net | LABS | State Machines | NeMe @ github | Covid-19; a resource
I do my best editing after I click on the submit button.

Offline

#19 2020-07-27 10:15:31

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 9,637
Website

Re: [SOLVED] trim and then replace

You need to use parse() here, as I did in my original example:

$remoteFile = parse('<txp:variable name="remote_file" />');

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

#20 2020-07-27 14:15:09

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 8,159
Website

Re: [SOLVED] trim and then replace

Bloke wrote #324894:

You need to use parse() here, as I did in my original example:

$remoteFile = parse('<txp:variable name="remote_file" />');...

Thanks, that indeed, did do it.


Yiannis
——————————
neme.org | hblack.net | LABS | State Machines | NeMe @ github | Covid-19; a resource
I do my best editing after I click on the submit button.

Offline

Board footer

Powered by FluxBB