0

Everyone know the $_SERVER['REQUEST_URI'] returns the parent directory and file name ex: directory/class/method <-- MVC Pattern.
I want $_SERVER['REQUEST_URI'] returns like $_SERVER['PATH_INFO'].
I know maybe someone says why you don't use $_SERVER['PATH_INFO'] ?
because the web server unfortunately does not support PATH_INFO.

I want class/method not directory/class/method.

Note that the directory may be present and maybe not. Therefore must check first if the directory present or not.

Lion King
  • 32,851
  • 25
  • 81
  • 143
  • Have you tried some code? Please include it in the question so that we can check. – silverstrike Jun 26 '16 at 03:37
  • @silverstrike: Unfortunately I have no idea to do that, but I know the steps : check if the directory is present or not, if present, remove it , and if not, doesn't do anything. – Lion King Jun 26 '16 at 03:49
  • maybe interesting? http://stackoverflow.com/questions/5598480/php-parse-current-url – Ryan Vincent Jun 26 '16 at 13:00

2 Answers2

0

Have you tried:

$path = explode('/',$_SERVER['REQUEST_URI'],2)[1];

The 2 is the limit of the array elements and the [1] is a shortcut to return the second element (array dereferencing). You may have to tweak these values to get what you want.

Alternative version for unsupported or old PHP versions that do not support the array dereferencing (which was added in 5.4.x).

$raw = explode('/',$_SERVER['REQUEST_URI'],2);
$path = $raw[1];
Tigger
  • 8,980
  • 5
  • 36
  • 40
  • Thank you, but unfortunately, it does not works. The input (`http://localhost/directory/welcome/greeting`), the output `Array ( [0] => / ) `. also please see the added updates in question – Lion King Jun 26 '16 at 03:27
  • Then you did not use the code I posted or something is very different at you end. What PHP version are you using (it would have to be old for this not to work). – Tigger Jun 26 '16 at 03:48
  • PHP 5.3.27 and PHP 5.4.16 doing the same thing. – Lion King Jun 26 '16 at 03:50
  • I have no idea how `$_SERVER[REQUEST_URI']` is returning the full URL. It should only be returning what is after the domain. PHP 5.5.x is the current lowest supported version and should work with the above code. Is there a reason why you are running an unsupported version of PHP? – Tigger Jun 26 '16 at 06:22
0

I have checked Tigger's answer. Can you try this one:

$url = str_replace('http://', '', 'http://localhost/directory/welcome/greeting');

$path = explode('/',$url,3)[2];
echo $path;

I needed to remove http:// or https://

  • Thank you, but I don't want to replace a string or something like that, I want to perform these steps, check if the directory is present in `$_SERVER['REQUEST_URI']` or not, if present, remove it , and if not, doesn't do anything. – Lion King Jun 26 '16 at 18:58