19

I need to get the very last word from an URL. So for example I have the following URL:

http://www.mydomainname.com/m/groups/view/test

I need to get with PHP only "test", nothing else. I tried to use something like this:

$words = explode(' ', $_SERVER['REQUEST_URI']);
$showword = trim($words[count($words) - 1], '/');
echo $showword;

It does not work for me. Can you help me please?

Thank you so much!!

Demento
  • 4,039
  • 3
  • 26
  • 36
DiegoP.
  • 45,177
  • 34
  • 89
  • 107
  • 1
    What did it echo. Also why are you exploding the url between spaces, you should have explode('/', $_SERVER['REQUEST_URI']); – Chris May 07 '11 at 12:40

8 Answers8

47

Use basename with parse_url:

echo basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
sanjary
  • 1,206
  • 1
  • 9
  • 10
25

by using regex:

preg_match("/[^\/]+$/", "http://www.mydomainname.com/m/groups/view/test", $matches);
$last_word = $matches[0]; // test
fardjad
  • 20,031
  • 6
  • 53
  • 68
4

I used this:

$lastWord = substr($url, strrpos($url, '/') + 1);

Thnx to: https://stackoverflow.com/a/1361752/4189000

Community
  • 1
  • 1
Wouter den Ouden
  • 1,523
  • 2
  • 17
  • 44
2

You can use explode but you need to use / as delimiter:

$segments = explode('/', $_SERVER['REQUEST_URI']);

Note that $_SERVER['REQUEST_URI'] can contain the query string if the current URI has one. In that case you should use parse_url before to only get the path:

$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

And to take trailing slashes into account, you can use rtrim to remove them before splitting it into its segments using explode. So:

$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = explode('/', rtrim($_SERVER['REQUEST_URI_PATH'], '/'));
Gumbo
  • 643,351
  • 109
  • 780
  • 844
1

If you don't mind a query string being included when present, then just use basename. You don't need to use parse_url as well.

$url = 'http://www.mydomainname.com/m/groups/view/test';
$showword = basename($url);
echo htmlspecialchars($showword);

When the $url variable is generated from user input or from $_SERVER['REQUEST_URI']; before using echo use htmlspecialchars or htmlentities, otherwise users could add html tags or run JavaScript on the webpage.

Dan Bray
  • 7,242
  • 3
  • 52
  • 70
1

To do that you can use explode on your REQUEST_URI.I've made some simple function:

function getLast()
{
    $requestUri = $_SERVER['REQUEST_URI'];

   # Remove query string
    $requestUri = trim(strstr($requestUri, '?', true), '/');
   # Note that delimeter is '/'
    $arr = explode('/', $requestUri);
    $count = count($arr);

    return $arr[$count - 1];
}

echo getLast();
OskarD90
  • 613
  • 2
  • 8
  • 26
Robik
  • 6,047
  • 4
  • 31
  • 41
  • Thanks everybody it works, but now I have another problem that I will explain in another post. Thanks – DiegoP. May 07 '11 at 13:49
  • I see you're new here. Fell free to accept some answer here and vode some up/down. You can for example vote up all answers here. – Robik May 07 '11 at 14:09
0

use preg*

if ( preg_match( "~/(.*?)$~msi", $_SERVER[ "REQUEST_URI" ], $vv ))
 echo $vv[1];
else
 echo "Nothing here";

this was just idea of code. It can be rewriten in function.

PS. Generally i use mod_rewrite to handle this... ans process in php the $_GET variables. And this is good practice, IMHO

publikz.com
  • 931
  • 1
  • 12
  • 22
-2
ex: $url      = 'http://www.youtube.com/embed/ADU0QnQ4eDs';
$url      = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url_path = parse_url($url, PHP_URL_PATH);
$basename = pathinfo($url_path, PATHINFO_BASENAME);
// **output**: $basename is "ADU0QnQ4eDs"

complete solution you will get in the below link. i just found to Get last word from URL after a slash in PHP.

Get last parameter of url in php

Community
  • 1
  • 1