Find last character in a string in PHP

前端 未结 7 1881
粉色の甜心
粉色の甜心 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:50

    You can use substr:

    substr($str, -1)
    

    This returns the last byte/character in a single-byte string. See also the multi-byte string variant mb_substr.

    But if you just want to remove any trailing slashes, rtrim is probably the best solution.

    And since you’re working with URLs, you might also take a look at parse_url to parse URLs as a trailing slash does not need to be part of the URL path.

提交回复
热议问题