Php removes the last "/" from the path

this is my code

$method = $_SERVER['PATH_INFO']; 

and this is my way:

 http://localhost:8082/XXXX/controllers/User.php/newUser?name=hello 

the result of the method is /newUser

I would like to have only newUser . IE without /

could you help me?

+5
source share
4 answers
 $method = ltrim($_SERVER['PATH_INFO'], '/'); 
+4
source

use ltrim for the variable you want? It seems to me the easiest way

 $var = ltrim($var,"/"); 
+6
source
 $withoutSlash = substr($_SERVER['PATH_INFO'], 1); 
+3
source

Your question may already exist:

URL: PHP How to remove the last part of the path

One solution:

  preg_replace("/\/\w+$/i","",__DIR__); # Note you may also need to add .DIRECTORY_SEPARATOR at the end. 

Another solution:

 dirname($path) Documentation: http://ca3.php.net/dirname 
+3
source

Source: https://habr.com/ru/post/1208814/


All Articles