字符串在最右边的字符后替换

时间:2014-12-04 05:34:03

标签: php string replace preg-replace substring

这是我的角色

/public/proj/index.php/home
/public/proj/index.php/test
/public/proj/home
/public/proj/test

我只需要在最右边的字符后面替换字符' /'登录。所以它将是

/public/proj/index.php/login
/public/proj/index.php/login
/public/proj/login
/public/proj/login

2 个答案:

答案 0 :(得分:2)

试试这个 -

$str = "/public/proj/index.php/home";
$vals = explode('/', $str);
$vals[count($vals)-1] = 'login';
$str = implode('/', $vals);
var_dump($str);

答案 1 :(得分:2)

这应该适合你:

$str = "/public/proj/home";
echo $str = substr_replace($str, "login", -1*(strlen(basename($str))), strlen(basename($str)));

输出:

/public/proj/login