0

I've been working with this code

<?php
class PerchTemplateFilter_sol_en_cat_path extends PerchTemplateFilter {
    public function filterAfterProcessing($value, $valueIsMarkup = false) {

        // ORIGINAL STRING: solutions-en/rail-technologies/track-components/name-of-product
        $mystring = $value;
        $replace = ['solutions-en', '%2F'];
        $str = '';

        $oldstr = str_replace($replace, $str, $mystring);

        $str_to_insert = 'XXX';
        $findme = '/';
        $pos = strpos($mystring, $findme); // I NEED THIS TO INSERT $str_to_insert AFTER THE SECOND FORWARD SLASH FOUND IN THE ORIGINAL STRING?

        $value = substr_replace($oldstr, $str_to_insert, $pos, 0);

        return $value;

        // $value: /rail-technologies/track-components/XXX/name-of-product

        // Insert string at specified position
        // https://stackoverflow.com/questions/8251426/insert-string-at-specified-position
    }
}
PerchSystem::register_template_filter('sol_en_cat_path', 'PerchTemplateFilter_sol_en_cat_path');

?>

My string is: solutions-en/rail-technologies/track-components/name-of-product

I want to end up with: /rail-technologies/XXX/track-components/name-of-product

XXX is only a placeholder value

I guess I need to do something with $pos to set where I want XXX to be added to the string.

I need to insert after the second forward slash, as the string may contain different text

The code above outputs this string: /rail-technoXXXlogies/track-components/ewosr-switch-lock

I can't seem to figure out how to insert XXX after the second forward slash.

Hope someone can provide some help.

StephenMeehan
  • 1,083
  • 2
  • 15
  • 26

2 Answers2

2

How about explode to array, then implode the first two items.
Join with xxx and implode the rest?

function AddInTheMiddle($start, $where, $what){
    $arr = explode("/", $what);
    $str = implode("/", array_splice($arr,$start,$where)) . '/xxx/' . implode("/", $arr);;

    return $str;
}

$str = 'solutions-en/rail-technologies/track-components/name-of-product';
$str = AddInTheMiddle(1, 2, $str);

https://3v4l.org/m98io

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • @omar if you see this, I think your answer is correct. – Andreas Apr 24 '20 at 15:11
  • Thank you, after answering I realized question was not clear enough I guess because in code section commented line the `xxxx` value was on the different position and in the explanation it was on a different place. anyway if someone deletes their answer does it stay visible ? I'm confused. – Umer Abbas Apr 24 '20 at 15:24
  • 1
    @OmarAbbas no it's not visible for everyone, but I believe 10.000+ can see them. Can't remember what limit it is but I believe it's 10.000. We see the answer with a red background-ish... – Andreas Apr 24 '20 at 16:07
0

Thank you Andreas, your post gave me the nudge I needed. I did this in the end.

// ORIGINAL $value: solutions-en/rail-technologies/track-components/name-of-product

        $str = explode("/", $value);
        $value = $str[1] . '/' . 'solutions' . '/' . $str[2] . '/';
        return $value;

        // Removed: solutions-en
        // Added: solutions
        // $value: rail-technologies/solutions/track-components/name-of-product

I was able to add the name-of-product to the end of the new string elsewhere in my template.

StephenMeehan
  • 1,083
  • 2
  • 15
  • 26
  • Ok, so you really didn't want [0]? I thought it was a typo. Anyways you are hardcoding stuff. I strongly recommend not doing so use the "2" as a variable that you can pass to your function together with the other variables. That way you can use this to more than this specific case. What if there is one more subdirectory? I'll edit my answer to show you what I mean. – Andreas Apr 24 '20 at 21:40
  • Have a look now. There is now three variables controlling the output. So even if the string is `solutions-en/rail-technologies/track-components/name-of-product/ somethingMore/2020/version2` then it will still work – Andreas Apr 24 '20 at 21:49