83

I'm just wondering how I can extract the last part of a URL using PHP.

The example URL is:

http://domain.example/artist/song/music-videos/song-title/9393903

Now how can I extract the final part using PHP?

9393903

There is always the same number of variables in the URL, and the id is always at the end.

14 Answers 14

186

The absolute simplest way to accomplish this, is with basename()

echo basename('http://example.com/artist/song/music-videos/song-title/9393903');

Which will print

9393903

Of course, if there is a query string at the end it will be included in the returned value, in which case the accepted answer is a better solution.

1
  • seriously. After many regular expressions I came around this answer. Why didn't I find this earlier. Plain and simple. Oct 8, 2020 at 18:42
107

Split it apart and get the last element:

$end = end(explode('/', $url));
# or:
$end = array_slice(explode('/', $url), -1)[0];

Edit: To support apache-style-canonical URLs, rtrim is handy:

$end = end(explode('/', rtrim($url, '/')));
# or:
$end = array_slice(explode('/', rtrim($url, '/')), -1)[0];

A different example which might me considered more readable is (Demo):

$path = parse_url($url, PHP_URL_PATH);
$pathFragments = explode('/', $path);
$end = end($pathFragments);

This example also takes into account to only work on the path of the URL.


Yet another edit (years after), canonicalization and easy UTF-8 alternative use included (via PCRE regular expression in PHP):

<?php

use function call_user_func as f;
use UnexpectedValueException as e;

$url = 'http://example.com/artist/song/music-videos/song-title/9393903';

$result = preg_match('(([^/]*)/*$)', $url, $m)

    ? $m[1]
    : f(function() use ($url) {throw new e("pattern on '$url'");})
    ;

var_dump($result); # string(7) "9393903"

Which is pretty rough but shows how to wrap this this within a preg_match call for finer-grained control via PCRE regular expression pattern. To add some sense to this bare-metal example, it should be wrapped inside a function of its' own (which would also make the aliasing superfluous). Just presented this way for brevity.

6
  • 1
    Mind the gap: This code exploits something that could be considered a bug in PHP. I'll update the answer.
    – hakre
    Sep 13, 2011 at 0:05
  • 2
    @hakre +1, but you'll get nothing if there's trailing slash in the $url. Better use array_filter to remove empty elements first — $pathFragments = array_filter(explode('/', $path));
    – jibiel
    Mar 15, 2013 at 15:47
  • 1
    @jibiel: Or do the rtrim-boogie, I edited the answer, see the now third code-example.
    – hakre
    Mar 15, 2013 at 16:35
  • This solution probably performs somewhere in between the basename/str* solutions and the preg solutions if I had to guess. While it's an intuitively simple approach, it comes with some runtime overhead. Nov 15, 2014 at 20:48
  • 1
    @hakre Demo link is broken. Please kindly fix it Jun 21, 2017 at 11:37
32

If you are looking for a robust version that can deal with any form of URLs, this should do nicely:

<?php

$url = "http://foobar.example/foo/bar/1?baz=qux#fragment/foo";
$lastSegment = basename(parse_url($url, PHP_URL_PATH));
2
  • 1
    This is my favorite of the bunch. Robust, performant and concise. Nov 15, 2014 at 20:44
  • Much easier than the other ones
    – balping
    Sep 13, 2015 at 15:07
28

You can use preg_match to match the part of the URL that you want.

In this case, since the pattern is easy, we're looking for a forward slash (\/ and we have to escape it since the forward slash denotes the beginning and end of the regular expression pattern), along with one or more digits (\d+) at the very end of the string ($). The parentheses around the \d+ are used for capturing the piece that we want: namely the end. We then assign the ending that we want ($end) to $matches[1] (not $matches[0], since that is the same as $url (ie the entire string)).

$url='http://domain.example/artist/song/music-videos/song-title/9393903';

if(preg_match("/\/(\d+)$/",$url,$matches))
{
  $end=$matches[1];
}
else
{
  //Your URL didn't match.  This may or may not be a bad thing.
}

Note: You may or may not want to add some more sophistication to this regular expression. For example, if you know that your URL strings will always start with http:// then the regex can become /^http:\/\/.*\/(\d+)$/ (where .* means zero or more characters (that aren't the newline character)).

4
  • Nice. Could also be done with preg_replace in 1 line. I'll add an answer. Oct 24, 2014 at 17:50
  • 3
    This solution is probably the slowest of the bunch; not hating, just saying. Nov 15, 2014 at 20:45
  • 1
    What about example.com/123/my-slug/1235 and you needed 1235?
    – zanderwar
    Jan 22, 2017 at 7:40
  • It works on strings. But how do you get that last part from URL?
    – Cornelius
    Feb 11, 2020 at 21:18
15
$id = strrchr($url,"/");
$id = substr($id,1,strlen($id));

Here is the description of the strrchr function: http://www.php.net/manual/en/function.strrchr.php

3
  • This solution is likely the fastest of the bunch, save perhaps the basename() solution when applicable. Nov 15, 2014 at 20:37
  • this solution is very helpful and very simple Oct 28, 2016 at 18:58
  • The solution is very helpful for me too
    – STA
    Jun 6, 2021 at 15:55
7

Another option:

$urlarray=explode("/",$url);
$end=$urlarray[count($urlarray)-1];
6

One of the most elegant solutions was here Get characters after last / in url

by DisgruntledGoat

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

strrpos gets the position of the last occurrence of the slash; substr returns everything after that position.

3

One liner: $page_path = end(explode('/', trim($_SERVER['REQUEST_URI'], '/')));

Get URI, trim slashes, convert to array, grab last part

1
3

A fail safe solution would be:

Referenced from https://stackoverflow.com/a/2273328/2062851

function getLastPathSegment($url) {
    $path = parse_url($url, PHP_URL_PATH); // to get the path from a whole URL
    $pathTrimmed = trim($path, '/'); // normalise with no leading or trailing slash
    $pathTokens = explode('/', $pathTrimmed); // get segments delimited by a slash

    if (substr($path, -1) !== '/') {
        array_pop($pathTokens);
    }
    return end($pathTokens); // get the last segment
}

echo getLastPathSegment($_SERVER['REQUEST_URI']); //9393903
1
$mylink = $_SERVER['PHP_SELF'];
$link_array = explode('/',$mylink);
echo $lastpart = end($link_array);
1
function getLastPathSegment($url) {
    $arr = explode('/', $url);
    return $arr[count($arr) - 1];
}
1
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. Jul 10, 2021 at 22:23
1

this will do the job easily to get the last part of the required URL

$url="http://domain.example/artist/song/music-videos/song-title/9393903";
$requred_string= substr(strrchr($url, "/"), 1);

this will get you the string after first "/" from the right.

1
  • Do mind: if there's a trailing slash at the end this will fail. See the comments at the solution of hakre and his edit. Check out Peter's solution: the basename() function is made for this.
    – metatron
    Feb 9, 2016 at 11:03
0

1-liner

$end = preg_replace( '%^(.+)/%', '', $url );

// if( ! $end ) no match.

This simply removes everything before the last slash, including it.

0

One line working answer:

$url = "http://www.yoursite/one/two/three/drink";
echo $end = end((explode('/', $url)));

Output: drink

1
  • Ho do i get only two/three/drink from the url ?
    – Sri
    Sep 18, 2018 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.