154

I want to get the characters after the last / in an url like http://www.vimeo.com/1234567

How do I do with php?

1

10 Answers 10

319

Very simply:

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

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


As mentioned by redanimalwar if there is no slash this doesn't work correctly since strrpos returns false. Here's a more robust version:

$pos = strrpos($url, '/');
$id = $pos === false ? $url : substr($url, $pos + 1);
4
  • This cuts of the first character if there is no slash at all. Nov 2, 2015 at 13:55
  • @redanimalwar I added another solution Nov 2, 2015 at 14:56
  • 11
    I am wondering why the $str = basename($url) is not getting more upvotes, works perfectly fine for me. Nov 2, 2015 at 16:39
  • 3
    @redanimalwar (1) basename is intended for file paths, not URLs - I think it will always return the same result but there may be edge cases like backslashes; (2) basename only works for slashes whereas my answer can be easily modified if someone else wants to do the same thing with a different character. Nov 2, 2015 at 20:39
66

Check out basename(). It should work like this:

$string = basename($url);
3
  • 2
    Does this work on urls? Isn't basename intended for file paths? Apr 5, 2013 at 18:24
  • 3
    Seems to work fine, It's a string function; it doesn't check if the path exists.
    – Gifford N.
    Mar 24, 2016 at 17:06
  • Careful if you use this on strings other than URLs, e.g. classnames (__ CLASS __). basename() will work on Windows as the DIRECTORY_SEPARATOR is a backslash, but on Linux, basename() only looks for a slash, so basename('App\Class') will return "Class" on Windows but "App\Class" on Linux.
    – Select0r
    Jul 2, 2021 at 6:45
13

You could explode based on "/", and return the last entry:

print end( explode( "/", "http://www.vimeo.com/1234567" ) );

That's based on blowing the string apart, something that isn't necessary if you know the pattern of the string itself will not soon be changing. You could, alternatively, use a regular expression to locate that value at the end of the string:

$url = "http://www.vimeo.com/1234567";

if ( preg_match( "/\d+$/", $url, $matches ) ) {
    print $matches[0];
}
5
  • explode always seems like more overhead to me, though I haven't ever timed it to see how fast it is. Sep 1, 2009 at 10:47
  • It appears explode() is a bit slower. On 10k instances, this is the amount of time taken for both. substr() first: 0.013657/0.045038
    – Sampson
    Sep 1, 2009 at 10:54
  • Might be slower, but I prefer the explode here. Specifically if the url doesn't contain any "/" the +1 in the strrpos answer makes a mess explode is able to overcome.
    – Noam
    May 15, 2014 at 8:28
  • 2
    The example throws a notice and should definetly be re-worked.
    – Xatenev
    Nov 22, 2016 at 13:57
  • 1
    Yes, exploded data must be a variable
    – ymakux
    Sep 30, 2017 at 15:38
12

You can use substr and strrchr:

$url = 'http://www.vimeo.com/1234567';
$str = substr(strrchr($url, '/'), 1);
echo $str;      // Output: 1234567
1
  • More like this : ltrim(strrchr($url,"/"),"/"); strchr return a string, not a number Mar 26, 2015 at 10:24
10
$str = "http://www.vimeo.com/1234567";
$s = explode("/",$str);
print end($s);
2
  • 3
    my version, although the end result is the same as the one posted, enables OP to use the other items of the split up string if he wished to.
    – ghostdog74
    Sep 1, 2009 at 11:26
  • this is the correct answer, since the title asks after 'last' / in url Aug 26, 2017 at 18:26
1

array_pop(explode("/", "http://vimeo.com/1234567")); will return the last element of the example url

1
1
Str::afterLast($url, '/');

A helper method in Laravel since Laravel 6.

0

Two one liners - I suspect the first one is faster but second one is prettier and unlike end() and array_pop(), you can pass the result of a function directly to current() without generating any notice or warning since it doesn't move the pointer or alter the array.

$var = 'http://www.vimeo.com/1234567';

// VERSION 1 - one liner simmilar to DisgruntledGoat's answer above
echo substr($a,(strrpos($var,'/') !== false ? strrpos($var,'/') + 1 : 0));

// VERSION 2 - explode, reverse the array, get the first index.
echo current(array_reverse(explode('/',$var)));
1
  • @redburn, Using end() this way in php 5.4+ and up will throw a notice. According to docs: "you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.". May 7, 2020 at 17:15
0

My version is:

trim(strrchr($url, "/"), "/");

https://www.php.net/manual/function.strrchr

-1

Here's a beautiful dynamic function I wrote to remove last part of url or path.

/**
 * remove the last directories
 *
 * @param $path the path
 * @param $level number of directories to remove
 *
 * @return string
 */
private function removeLastDir($path, $level)
{
    if(is_int($level) && $level > 0){
        $path = preg_replace('#\/[^/]*$#', '', $path);
        return $this->removeLastDir($path, (int) $level - 1);
    }
    return $path;
}

Your Answer

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

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