You could come to a solution using several methods.
PHP allows you to use a set of "magic" arrays and the one we are particularly interested in is $_SERVER.
This array has a number of values that you might want to explorer further.
Lets start with http://, we will use getenv('HTTPS') if we are using https:// then it will return 'on' otherwise it will return false.
$base = ( getenv('HTTPS') == 'on' ) ? 'https://' : 'http://';
now we can see what is the "domain" that we are using, so we can use $SERVER['SERVERNAME'] which will return 'www.domain.com' or what ever that we want.
$base .= $SERVER['SERVERNAME'];
Next we have several options to extract the part after http://www.domain.com/ so we will use str_replace
$string_to_extract = str_replace($base,'',$url);
Hopefully it helps and I will be more then glad to answer further.