Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

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?
0
Eternal_Student
Asked:
Eternal_Student
  • 7
  • 4
  • 3
  • +1
1 Solution
 
effxCommented:
Try:
<?php
$arrUrl = explode("/", $_SERVER['REQUEST_URI']);
$lastPart = array_pop($arrUrl);
print $lastPart;
?>

Open in new window

0
 
Ovid BurkeCreative DirectorCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ovid BurkeCreative DirectorCommented:
:)

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
 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now