Remove Trailing Slash From String PHP

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

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

相关标签:
5条回答
  • 2020-11-30 07:33

    rtrim Use rtrim cause it respects the string doesnt end with a trailing slash

    0 讨论(0)
  • 2020-11-30 07:35

    This removes trailing slashes:

    $str = rtrim($str, '/');
    
    0 讨论(0)
  • 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, '/');
    
    0 讨论(0)
  • 2020-11-30 07:42

    Yes, it is!

    http://php.net/manual/en/function.rtrim.php

    0 讨论(0)
  • 2020-11-30 07:53

    Long accepted, however in my related searches I stumbled here, and am adding for "completeness"; rtrim() is great, however implemented like this:

    $string = rtrim($string, '/\\'); //strip both forward and back slashes
    

    It ensures portability from *nix to Windows, as I assume this question pertains to dealing with paths.

    0 讨论(0)
提交回复
热议问题