Search
Other ReReplacer questions
Forum

Replace unknown number of matches

Tim Rinkel's Avatar Tim Rinkel
Hi Peter

I'm trying to build a ReReplacer item to build a <time> element. I have a basic version working:

Search string:
{tr-time '(.*?)' '(.*?)'}
Replace string:
<time datetime="\1">{source}<?php echo date(\2,strtotime("\1"))?>{/source}</time>

The first input is a valid date or datetime string and the second is a format string which may also be a ReReplacer item. So:
<p>The party is on {tr-time '2025-05-27' 'l, F j, Y'}.</p>

becomes:
<p>The party is on <time datetime="2025-05-27">Tuesday, May 27, 2025</time>.</p>

What I would like to do is reuse the input date whenever indicated in the code, like by another occurrence of a date format, until a closing tag is encountered. For example, something like:
<p>The party is on {tr-time '2025-05-27T19:00' 'F j, Y'} at {'g:i a'}. That's on a {'l'}{/tr-time}.</p>

Would return:
<p>The party is on <time datetime="2025-05-27T19:00">May 27, 2025</time> at 7:00 pm. That's on a Tuesday.</p>

What I can't figure out is how to pick an unknown number of format matches out of unknown text between the two guard strings. Any thoughts? 'Beyond this point there be dragons' is a perfectly acceptable answer.

Thanks
Peter van Westen's Avatar Peter van Westen ADMIN
There definitely be dragons.

This could be done in (at least) 2 ways, both of which I do not really recommend.

First way would be to create a couple of ReReplacer items.
- The first would search for a a starting {tr-time ...} tag and the first following format tag (before the end tag). Then replace that with your <time> tag + php code. It will repeat that replacement ('Thorough' option), till all nested format tags are replaced.
- The second will replace the initial {tr-time ...} tag.
- The third will get rid of the ending {/tr-time} tag.

The second way would be to create a search that will find the entire {tr-time ...}...{/tr-time} blocks.
Then in the replacement pass that to a php block which does all the magic you need. So find the initial tag with the date and format. And all the nested format tags. And replace all those using the initial format.

But, then you have pretty much coded a complete plugin. So then it may be better to just create a dedicated plugin for this.

PS: you do not have a subscription for ReReplacer. I do not offer support on the free versions. So if you need further help, please purchase a subscription for it.
Please post a rating at the Joomla! Extensions Directory
Tim Rinkel's Avatar Tim Rinkel
Sorry Peter, I thought this was one of your extensions I had a subscription for. Thanks for the information.
You can only post on the extension support forum if you have an active subscription and you log in

Buy a Pro subscription