Find last character in a string in PHP

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

    You can use basename()

    This will return characters for http://domainx.com/characters/ as well as http://domainx.com/characters

    You can do like this:-

    $page = $_SERVER['REQUEST_URI'];
    $module = basename($page);
    

    Then you can use the $module directly in your conditional logic without doing any redirects.

    If you want to collect the last / trimmed URL then you can do this:-

    If you are storing the project base url in a config file:-

    BASE_URL  = 'http://example.com'
    

    then you can do this:-

    $page = $_SERVER['REQUEST_URI'];
    $module = basename($page);
    $trimmedUrl = BASE_URL.'/'.$module;
    

提交回复
热议问题