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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

Problem transforming key/word in admin plugin

Solved by Karmalakas View solution

Started by Junillon 1 year ago · 5 replies · 80 views
1 year 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 ?

1 year 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']

1 year ago

perfect summary.

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

1 year 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)

1 year 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
1 year 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 Rene, 1 week ago
2 42 1 week ago
Plugins · by Xavier, 4 weeks ago
2 53 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1179 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 47 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 71 2 months ago