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

前端 未结 4 1221
闹比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条回答
  •  暖寄归人
    2020-12-29 18:30

    My solution: simple and even converts back slashes, useful for windows developers:

    function fixpath($p) {
        $p=str_replace('\\','/',trim($p));
        return (substr($p,-1)!='/') ? $p.='/' : $p;
    }
    

提交回复
热议问题