Search code examples
phpvariablestrailing-slash

Remove "/" from the end of a variable


Can any one please tell me how I can remove the slash from the end of a variable

In my index.php I have the following:

$url=$_SERVER['REQUEST_URI'];
include_once "sites/$url.php";

My problem is if I write example.com/test/somefile/ nothing comes but if I write example.com/test/somefile it works

So is there a way to remove the slash if the variable ends with a slash?


Solution

  • Try this

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

    From PHP.net https://www.php.net/rtrim

    You can also specify the characters you want to strip, by means of the character_mask parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters.

    While this will solve your problem, please take a few minutes to consider the warnings posted in the comments and the other answer(s) regarding code injection since it is a very serious security issue.