Find last character in a string in PHP

前端 未结 7 1899
粉色の甜心
粉色の甜心 2020-12-15 09:02

I\'m doing some url rewriting in PHP and need to find URLS with a slash at the end and then do a 301 redirect. I thought there\'d be a simple PHP function to find last stri

7条回答
  •  别那么骄傲
    2020-12-15 09:55

    A nice solution to remove safely the last / is to use

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

    rtrim() removes all /s on the right side of the string when there is one or more.

    You can also safely add exactly one single / at the end of an URL:

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

提交回复
热议问题