Search code examples
phpgeturl

Get part of the current url PHP


How i cant get a specific part of the current url? for example, my current url is:

http://something.com/index.php?path=/something1/something2/something3/

Well, i need to print something2 with php.

Thanks!


Solution

  • You use the explode function in PHP to separate the URL by the first parameter (in this case a forward slash). To achieve your goal you could use;

    $url = "http://something.com/index.php?path=/something1/something2/something3/";
    $parts = explode('/', $url);
    $value = $parts[count($parts) - 2];