Marco Dinatsoli
Marco Dinatsoli

Reputation: 10590

php remove the last "/" from the path

this is my code

 $method = $_SERVER['PATH_INFO'];

and this is my path:

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

the result of the method is /newUser

I would like to have just newUser. IE without the /

could you help me please

Upvotes: 5

Views: 483

Answers (4)

MartaGom
MartaGom

Reputation: 501

Maybe your question already exist:

URL: PHP How to remove last part of a 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

Upvotes: 2

exussum
exussum

Reputation: 18578

use ltrim on the variable you want ? Seems the easiest way to me

$var = ltrim($var,"/");

Upvotes: 6

Mark Reed
Mark Reed

Reputation: 95375

$withoutSlash = substr($_SERVER['PATH_INFO'], 1);

Upvotes: 3

Legionar
Legionar

Reputation: 7607

$method = ltrim($_SERVER['PATH_INFO'], '/');

Upvotes: 4

Related Questions