Post by trujve » Sun Dec 03, 2023 7:22 am

Hej again,

I noticed a very strange behavior in my extension

When I install my extension, I noticed that some "str_replace" functions do not work with the "before" handler. But when I upload the corresponding controller file via FTP, everything works again!

Looking deeper into the code, I noticed that it is only the functions where I try to replace special code with "str_replace".

I think it is due to the indentation of the code which is lost during the regular installation and takes effect again during the transfer via FTP.
This works for example:

Code: Select all

$output = str_replace('</a></h4>',  'REPLACE WITH SOMETHING',  $output);

This example code does not work at first after installation, only when I transfer it via FTP:

Code: Select all

$output = str_replace('{% if review_status %}
            <li class="nav-item"><a href="#tab-review" data-bs-toggle="tab" class="nav-link">{{ tab_review }}</a></li>
          {% endif %}',  'REPLACE WITH SOMETHING',  $output);
Is there a solution for such cases? Sometimes you want to change more!

Best,
Jack

New member

Posts

Joined
Tue Jul 09, 2019 7:25 pm

Post by ADD Creative » Sun Dec 03, 2023 11:41 pm

Could be different line endings that is the issue.

www.add-creative.co.uk


Guru Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by trujve » Mon Dec 04, 2023 12:05 am

ADD Creative wrote:
Sun Dec 03, 2023 11:41 pm
Could be different line endings that is the issue.
Thnx.
But how should I imagine this if it is the same file, only one is uploaded via the normal installation routine and the other via FTP?

New member

Posts

Joined
Tue Jul 09, 2019 7:25 pm

Post by ADD Creative » Mon Dec 04, 2023 3:01 am

Some FTP applications will be set to change line endings. So I both files were not uploaded by the same FTP application using the same settings there could be a difference. Check the file size, as this will tell you if there is a difference.

www.add-creative.co.uk


Guru Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by trujve » Mon Dec 04, 2023 4:59 am

I don't think I've explained the issue properly.
Only when I upload the exact same controller file via FTP as was previously uploaded via the OC4 installation routine does the "str_replace" function work!

Jack

New member

Posts

Joined
Tue Jul 09, 2019 7:25 pm

Post by ADD Creative » Mon Dec 04, 2023 6:30 am

Yes, that is what I understood. You example string replace that doesn't work has line breaks in. Line brakes are not always the same. Some FTP applications will be set to change line endings. So this could be uploading the files with different line breaks to the OpenCart uploader.

www.add-creative.co.uk


Guru Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by trujve » Mon Dec 04, 2023 2:44 pm

ADD Creative wrote:
Mon Dec 04, 2023 6:30 am
Yes, that is what I understood. You example string replace that doesn't work has line breaks in. Line brakes are not always the same. Some FTP applications will be set to change line endings. So this could be uploading the files with different line breaks to the OpenCart uploader.
I know the FTP dilemma. But that's not what this is about.

And this is exactly where my big problem lies. I have no problems with the FTP upload of the controller file. If I upload them later via FTP everything works.

The problem occurs immediately after installing the extension. And this contains exactly the same file. It doesn't work there. It only works after uploading via FTP.

New member

Posts

Joined
Tue Jul 09, 2019 7:25 pm

Post by ADD Creative » Mon Dec 04, 2023 5:24 pm

Have you compared the the file between the version uploaded via the installer and the version uploaded via FTP? Where there any differences?

Have you tried using preg_replace?

www.add-creative.co.uk


Guru Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by trujve » Mon Dec 04, 2023 7:46 pm

ADD Creative wrote:
Mon Dec 04, 2023 5:24 pm
Have you compared the the file between the version uploaded via the installer and the version uploaded via FTP? Where there any differences?

Have you tried using preg_replace?
I compared them and they were "unfortunately" the same.

In fact, I hadn't even thought of preg_replace because I'm not that familiar with it yet. But that's a very good idea.
Any idea how this could look like?

New member

Posts

Joined
Tue Jul 09, 2019 7:25 pm

Post by ADD Creative » Mon Dec 04, 2023 9:11 pm

You could use \s+ to match the whitespace. You would also need to escape any special characters such as / in the string. So something like.

Code: Select all

$output = preg_replace('/{% if review_status %}\s*<li class="nav-item"><a href="#tab-review" data-bs-toggle="tab" class="nav-link">{{ tab_review }}<\/a><\/li>\s*{% endif %}/', 'REPLACE WITH SOMETHING',  $output);
Also be aware that preg_replace will not be as efficient as str_replace.

www.add-creative.co.uk


Guru Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by trujve » Mon Dec 04, 2023 9:17 pm

I looked a little deeper into preg_replace and ended up doing it this way.

Code: Select all

$do_something = preg_replace(
    '/<\/div>\s*<\/footer>\s*{{ cookie }}/',
    'SOMETHING<p>&nbsp;</p></div></footer>{{ cookie }}',
    $code
);
Many thanks for the great idea!

New member

Posts

Joined
Tue Jul 09, 2019 7:25 pm

Post by trujve » Mon Dec 04, 2023 9:18 pm

ADD Creative wrote:
Mon Dec 04, 2023 9:11 pm
You could use \s+ to match the whitespace. You would also need to escape any special characters such as / in the string. So something like.

Code: Select all

$output = preg_replace('/{% if review_status %}\s*<li class="nav-item"><a href="#tab-review" data-bs-toggle="tab" class="nav-link">{{ tab_review }}<\/a><\/li>\s*{% endif %}/', 'REPLACE WITH SOMETHING',  $output);
Also be aware that preg_replace will not be as efficient as str_replace.
:D many thanks

New member

Posts

Joined
Tue Jul 09, 2019 7:25 pm
Who is online

Users browsing this forum: Majestic-12 [Bot] and 5 guests