May 4, 2012
Get part of URL with PHP
Question by user1069269
I have a site and I would need to get one pages URL with PHP. The URL might be something www.mydomain.com/thestringineed/
or it can www.mydomain.com/thestringineed?data=1
or it can be www.mydomain.com/ss/thestringineed
So it’s always the last string but I dont want to get anything after ?
Answer by TecBrat
You will use the parse_url function, then look at the path portion of the return.
like this:
$url='www.mydomain.com/thestringineed?data=1';
$components=parse_url($url);
$mystring= end(explode('/',$components['path']));
Answer by Starx
parse_url()
is the function you are looking for. The exact part you want, can be received through PHP_URL_PATH
$url = 'http://php.net/manual/en/function.parse-url.php';
echo parse_url($url, PHP_URL_PATH);