Remove Trailing Slash From String PHP

前端 未结 5 1895
再見小時候
再見小時候 2020-11-30 07:28

Is it possible to remove the trailing slash / from a string using PHP?

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 07:38

    Sure it is, simply check if the last character is a slash and then nuke that one.

    if(substr($string, -1) == '/') {
        $string = substr($string, 0, -1);
    }
    

    Another (probably better) option would be using rtrim() - this one removes all trailing slashes:

    $string = rtrim($string, '/');
    

提交回复
热议问题