I am a complete beginner to PHP, so please keep that in mind.
I am attempting to get JUST the path from the current URL, which is variable. For instance, if the current URL is http://example.com/blog/2014/blogpost.html then I would like to get /blog/2014 or something very similar.
This is the PHP code I have so far:
$full_url = $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$path_info = parse_url($full_url,PHP_URL_PATH);
echo $path_info;
However, this does not work, and I can't seem to Google a suitable solution.
Does anyone have any pointers?
Thanks in advance!
You can use strrpos to get the last index of / and pull the part of the URI before that with:
$req_uri = $_SERVER['REQUEST_URI'];
$path = substr($req_uri,0,strrpos($req_uri,'/'));
This will give you exactly what you wanted (/blog/2014)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With