2014-03-29 42 views
1

后,如果我有以下URL作为一个字符串:添加一个字符串之前削减

www.example.com/img/1.png 

我怎么会添加字符串extra/在URL中的最后一个斜杠之后,那么其结果是:

www.example.com/img/extra/1.png 
+0

添加标签的.htaccess。我认为这是你需要解决这个问题的工具。它会引导可以在这里解决问题的人。 – Rimble

回答

2

使用preg_replace()

echo preg_replace('#(/[^/]+)(?=/[^/]+/?$)#', '$1/extra', $url); 

说明:

(  # group and capture to backreference $1 
/ # match literal '/' 
    [^/]+ # any character except: '/' (1 or more times) 
)  # end of capturing group 1 
(?=  # look ahead to see if there is: '/' 
    [^/]+ # any character except: '/' (1 or more times) 
    /?  # '/' (optional) 
    $  # assert position at the end of string 
)  # end of look-ahead 

Regex101 Demo

+1

感谢您的补充解释。 – user3390776

+0

@ user3390776:好的。很高兴我能帮上忙! –

相关问题