在php字符串的第二个正斜杠之后插入一些文本?

我一直在使用这段代码

<?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只是一个占位符值

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

我需要在第二个正斜杠之后插入,因为字符串可能包含不同的文本

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.

希望有人能提供帮助。

评论
  • 蛋小疼
    蛋小疼 回复

    爆炸成数组,然后内爆前两个项目。 加入xxx并让其他内爆?

    $str = 'solutions-en/rail-technologies/track-components/name-of-product';
    
    $arr = explode("/", $str);
    $str = implode("/", array_splice($arr,0,2)) . '/xxx/' . implode("/", $arr);;
    
    echo $str;
    // solutions-en/rail-technologies/xxx/name-of-product
    

    https://3v4l.org/m6loC