ReReplacer

Replacement title- & meta tag inside head not working

8 months 1 week ago #77953

Emiel's Avatar Emiel

Hello Peter.

First of all, thank you for your amazing products. Your extensions are super functional, very well and clean coded and you use a very fair policy on free vs. paid.

My current issue though:
I upgraded to pro, to be able to get re-replacements visible in some head tags as well. However after upgrading, it's not working.
After upgrading the option 'treat as php' disappeared too. I was expecting to be able to use that as well. But now it's gone.

I want to use re-replacer for article titles in Joomla as follows:

searchstring: {seizoen}

replacecode:

<?php if(date('n') < 10) {echo date("Y");} else {echo date("Y"); echo" en "; echo date("Y")+1;}?>

Output example: Goedkope vakantie Spanje 2018

So, obviously, after a certain date, the output of the year in the title should change.
I want this for the content, as well as for the <title> tag inside the head. The meta description tag is optional and not really needed.

How can I get this to work?
Many thanks in advance!

8 months 1 week ago #77957

Peter van Westen's Avatar Peter van Westen Admin

The PHP option will not show if you have specific text search options enabled, like the 'Treat as List' option.
Switch that off if it is on.

8 months 1 week ago #77961

Emiel's Avatar Emiel

Yep! That did it.
Easy peasy :)

Can I use an xml file instead, for the list of replacements?
{vorig jaar},{dit jaar},{volgend jaar},{afgelopen jaar},{komend jaar},{seizoen}

Or do I need to make seperate replacement items in the admin for this?

8 months 1 week ago #77962

Peter van Westen's Avatar Peter van Westen Admin

You can use an xml file for this.
Pretty much comes down to what you find easier to manage.

8 months 1 week ago #77963

Emiel's Avatar Emiel

Well, I asked, because the list option is not supported in combination with php in the admin.
So I wasn't sure if it would work, when I put everything in an xml. Just wanted to be sure, before I spend time on it :)

I guess, there is one more question on this:
Is there a difference in performance? Between admin items and use of xml file?
I maybe have a set of 20 to 30 replacements. I'm sure hard coding is always better for performance. But in these cases, making the replacement rules, just seem easier and more versatile for future use as well.

Anyway. Many thanks for your super fast and helpful support!
A-grade products from Regular Labs... Thumbs up!

8 months 1 week ago #77964

Emiel's Avatar Emiel

Oh... actually... I do have one more question. Sorry.
It's about language on date output.
My weather module returns: 10 February
It should be: 10 februari

This issue has been haunting me for years. I am sure it's a php server side thing. Since at one point it was working fine (for a while) on another website in the past.
But on my new server and with the new website I am making. This issue has popped up again.

I tried re-replacer on this. But it didn't work.

I use this code:

// Set PHP localisation to Dutch
setlocale(LC_TIME, 'nl_NL.utf-8');
<?php echo date("j F",strtotime($xml->forecast->time[2]['day'])); ?>

However... date output is still in english. And re-replacer can't replace it on the frontend. Any ideas on this?

8 months 1 week ago #77965

Peter van Westen's Avatar Peter van Westen Admin

No, you would not use the list option.
In the xml you would make a separate item for every search.

But you could combine this into one 'smart' replacement using Regular Expressions and php:

\{(vorig jaar|dit jaar|volgend jaar|afgelopen jaar|komend jaar|seizoen)\}
And in the replacement:
<?php
switch('\1') {
   case 'vorig jaar':
   case 'afgelopen jaar':
      echo date("Y")-1;
      break;

   case 'dit jaar':
      echo date("Y");
      break;

   case 'volgend jaar':
   case 'komend jaar':
      echo date("Y")+1;
      break;

   case 'seizoen':
      echo date("Y");
      if( date('n') > 10 ) {
          echo ' en ' . date("Y")+1;
      }
      break;
}
?>

8 months 1 week ago #77966

Peter van Westen's Avatar Peter van Westen Admin

Use the JDate object to make the date use the language setting:

$date = new JDate($xml->forecast->time[2]['day']);
echo $date->format("j F");

8 months 1 week ago #77967

Peter van Westen's Avatar Peter van Westen Admin

So you can also do something like this in your ReReplacer replacement:

<?php
switch('\1') {
   case 'vorig jaar':
   case 'afgelopen jaar':
      echo (new JDate('-1 year'))->format('Y');
      break;

   case 'dit jaar':
      echo (new JDate())->format('Y');
      break;

   case 'volgend jaar':
   case 'komend jaar':
      echo (new JDate('+1 year'))->format('Y');
      break;

   case 'seizoen':
      echo (new JDate())->format('Y');
      if(new JDate())->format('n') > 10 ) {
          echo ' en ' . (new JDate('+1 year'))->format('Y');
      }
      break;
}
?>

8 months 1 week ago #77968

Emiel's Avatar Emiel

In the xml you would make a separate item for every search.
B) Yes, doh, of course, That was a little silly of me to ask. My mind does not work in the logic of a programmer. (e.g. it took me a week to write the weather module for my site from openweathermap data source :unsure: :cheer: getting the data from the xml files into php was a big moment for me B) ) haha

I will try the regular expression approach. It's new to me, and I really can't think of these codes myself. but it seems like a more lightweight solution.
Many thanks for that!
$date = new JDate($xml->forecast->time[2]);
:ohmy: Seriously? Is that all that it would take? omg, I haven't been able to fix that issue for years.
You are my new hero!

Again many thanks.
Have a great weekend!
(I know I will now :cheer: )