PHP URL question

Hi - consider the following URL:

test.co.uk/site-name/section/paintings

How would I output the very last node of that URL? Baring in mind that the URL could also have additional notes?
LVL 18
Eternal_StudentAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

effxCommented:
Try:
<?php
$arrUrl = explode("/", $_SERVER['REQUEST_URI']);
$lastPart = array_pop($arrUrl);
print $lastPart;
?>

Open in new window

0
Ovid BurkeConsultant InstructorCommented:
Try this:

$url = $_SERVER['REQUEST_URI'];
$lastNode = basename($url);
echo $lastNode;

Open in new window

0
effxCommented:
You can compress my code to a single line if require, however my previous version allows you to acces other parts of the path unlike madaboutasp's solution
<?php
$lastPart = array_pop(explode("/", $_SERVER['REQUEST_URI']));
print $lastPart;
?>

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Ovid BurkeConsultant InstructorCommented:
:)

Perhaps you can also use pathinfo.

Examples:

$lastNode = pathinfo('test.co.uk/site-name/section/paintings', PATHINFO_FILENAME);

Open in new window


or

$lastNode = pathinfo('test.co.uk/site-name/section/paintings', PATHINFO_BASENAME);

Open in new window


Full documentatioin here: http://php.net/manual/en/function.pathinfo.php
0
Ray PaseurCommented:
the URL could also have additional notes?  What does that mean?  Can you clarify, please.  I am guessing that you want to get paintings out of test.co.uk/site-name/section/paintings.  Is that right?

What if the URL said this: test.co.uk/site-name/section/paintings/vanGogh/early/index.php ?
0
effxCommented:
@Ray, Probably a spelling mistake "notes"=="nodes"?
0
Ray PaseurCommented:
@effx: Yeah I think so, too.  Either way the structure of the URL and the exact output needs to be clarified.
0
Eternal_StudentAuthor Commented:
Yes I did mean nodes ... so for example there could be:

test.co.uk/site-name/section/paintings

OR

test.co.uk/site-name/section/paintings/van-gogh

In which case I would need the very last node.
0
effxCommented:
My solution woulr work
0
Ray PaseurCommented:
Man page reference:
http://php.net/manual/en/function.basename.php
http://php.net/manual/en/function.pathinfo.php

Here is how I have done it in the past:

$path = 'test.co.uk/site-name/section/paintings';
$node = end(explode(DIRECTORY_SEPARATOR, $path));

DIRECTORY_SEPARATOR is an OS-context-aware predefined constant in PHP, so either kind of slashes will work.
0
effxCommented:
it's a site URL, the seperators will always be "/"
0
Eternal_StudentAuthor Commented:
effx, your solution does work but I have ran into one slight problem. When the URL is:

test.co.uk/site-name/section/paintings?start=12

I only want to output paintings and not the ?start=12 part, how is this possible?
0
effxCommented:
:)

Pure sweetness:
<?php
$arrUrl = parse_url($_SERVER['REQUEST_URI']);
$subUrl = explode("/", $arrUrl["path"]);
$lastPart = array_pop($subUrl);
print $lastPart;
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eternal_StudentAuthor Commented:
Hi effx that is perfect, can you kindly explain how that bit of code works for me please?

Thanks
0
effxCommented:
$_SERVER['REQUEST_URI']

This shows the requested url eg. "http://www.test.com/dir/dir/dir/?query=blah&other=blah"

parse_url  (http://php.net/manual/en/function.parse-url.php)

parses the URL into its required parts from this I remove the Path variable

explode on a "/"

pop the last element of the end and return it

0
Eternal_StudentAuthor Commented:
It's this last bit here that I am failing to understand:

$lastPart = array_pop($subUrl);
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.