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

前端 未结 4 1231
闹比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:43

    you can try to use this function

        function endsWith($FullStr, $needle)
        {
            $StrLen = strlen($needle);
            $FullStrEnd = substr($FullStr, strlen($FullStr) - $StrLen);
            return $FullStrEnd == $needle;
        }
    

    taken from by blog post

    then use it like

    if (endsWith($path,'/') == false) 
    {
        $path = $path."/";
    }
    

    and offcourse if you do not want to use above function to siplify things then you should change the way you are using substr

    correct way to get last char is substr($path,-1)

提交回复
热议问题