Skip to content
Grav 2.0 is officially stable. Read the announcement →
Plugins

Problem transforming key/word in admin plugin

Solved by Karmalakas View solution

Started by Junillon 2 years ago · 5 replies · 157 views
2 years ago

Hi, I made an admin plugin storing some key/word options. The code is using it for a file modify & transfer from one server to another. Globally it works correctly, transferring with rsync and modifying with sed. (I thing I will push it to GIT if anyone is interested)
But I still have a problem that seems to be a contextual variable used and I don't understand how to bypass it.

When performing the action, the plugin
1 gets back the option (working)
$keywords = $this->pluginConfig->get('plugins.pagetransfer.avantapres') ?? [];
2 looping in
foreach ($keywords as $pair) {
3 at this step I need to replace caracters in ("/" must be "\/") so I tried with
$escapedKey=str_replace('/', '\\/', $pair['key']);
(also tryed with reg_replace, preg_replace...)
The result is no result ! Nothing changes. if $pair['key'] is equal to "http://www.google.com", $escapedKey is equal the same without any change.

After turning crazy, I tryed
$escapedKey=str_replace('/', '\\/', http://www.google.com);
and the result is good => http:\/\/www.google.com

Then as I think it may be contextual to all Grav layers, how can I overcome this behavior ?

2 years ago

So this works:

PHP
$escapedKey=str_replace('/', '\\/', http://www.google.com);

And this doesn't?

PHP
$escapedKey=str_replace('/', '\\/', $pair['key']);

Debug what you get as the $pair['key']

2 years ago

perfect summary.

$pair['key'] ="http://www.google.com"

2 years ago

ok a little more.
str_replace seems to work with changing something else than "/".
example :
based on$pair['key'] ="http://www.google.com"

if I go for $modify=str_replace('h', '\\/', $pair['key']);
I will have \/ttps:\\www.google.com

(and to answer the deleted post from @b.da , I'm using php 8.3)

2 years ago Solution

Either something is missing in your research or / symbols are different in $pair['key']. For example, all of these are different from each other:

TXT
/
̸
̷
last edited 01/09/25 by Karmalakas
2 years ago

you were damn right ! so stupid I am ! 😱 I don't know how I got those strange differents slash but that's it.
Thanks 😀

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Slebeig, 5 days ago
1 102 5 days ago
Plugins · by Rene, 4 weeks ago
2 303 4 weeks ago
Plugins · by Xavier, 2 months ago
2 224 1 month ago
Plugins · by Luka Prinčič, 7 years ago
3 1412 2 months ago
Plugins · by Sebastian van de Meer, 2 months ago
1 322 2 months ago