Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Get path after domain from current URL

Tags:

php

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!

like image 580
etsnyman Avatar asked Feb 18 '26 03:02

etsnyman


1 Answers

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)

like image 158
Ryan Willis Avatar answered Feb 19 '26 17:02

Ryan Willis