Get characters after last / in url

PhpString

Php Problem Overview


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

How do I do with php?

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

from os.path import basename

$str = basename($url);

Solution 3 - Php

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];
}

Solution 4 - Php

You can use substr and strrchr:

$url = 'http://www.vimeo.com/1234567';
$str = substr(strrchr($url, '/'), 1);
echo $str;      // Output: 1234567

Solution 5 - Php

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

Solution 6 - Php

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

Solution 7 - Php

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

Solution 8 - Php

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

A helper method in Laravel since Laravel 6.

Solution 9 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohanView Question on Stackoverflow
Solution 1 - PhpDisgruntledGoatView Answer on Stackoverflow
Solution 2 - PhpGZippView Answer on Stackoverflow
Solution 3 - PhpSampsonView Answer on Stackoverflow
Solution 4 - PhpGabrielView Answer on Stackoverflow
Solution 5 - Phpghostdog74View Answer on Stackoverflow
Solution 6 - Phpnikc.orgView Answer on Stackoverflow
Solution 7 - PhpEaten by a GrueView Answer on Stackoverflow
Solution 8 - PhpbradView Answer on Stackoverflow
Solution 9 - PhpMahmoud ZaltView Answer on Stackoverflow