php checking if the last character is a '/' if not then tack it on

前端 未结 4 1219
闹比i
闹比i 2020-12-29 17:56

I have these 2 snippets of code that I have been playing with, but can\'t seem to get the logic to stick in either of them.

I am trying to see if a given string has

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-29 18:47

    You might be overthinking it. While the substr() method will work perfectly it might be simpler to use rtrim() to remove any trailing slashes and then add one on.

    $path = rtrim($path, '/') . '/';
    

    Caution: this will trim multiple trailing forward slashes. so .////// becomes ./

提交回复
热议问题