Php get string after last slash

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

How do I do with php?

asked Sep 1, 2009 at 10:40

JohanJohan

18.5k30 gold badges69 silver badges88 bronze badges

1

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

answered Sep 1, 2009 at 10:43

DisgruntledGoatDisgruntledGoat

67.4k65 gold badges204 silver badges285 bronze badges

5

from os.path import basename

$str = basename[$url];

Pikamander2

6,2933 gold badges42 silver badges63 bronze badges

answered Sep 1, 2009 at 13:30

GZippGZipp

5,2481 gold badge21 silver badges16 bronze badges

3

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

print end[ explode[ "/", "//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 = "//www.vimeo.com/1234567";

if [ preg_match[ "/\d+$/", $url, $matches ] ] {
    print $matches[0];
}

answered Sep 1, 2009 at 10:41

SampsonSampson

261k74 gold badges530 silver badges559 bronze badges

5

You can use substr and strrchr:

$url = '//www.vimeo.com/1234567';
$str = substr[strrchr[$url, '/'], 1];
echo $str;      // Output: 1234567

SilentGhost

293k64 gold badges301 silver badges291 bronze badges

answered Sep 1, 2009 at 11:58

GabrielGabriel

6751 gold badge9 silver badges17 bronze badges

1

$str = "//www.vimeo.com/1234567";
$s = explode["/",$str];
print end[$s];

answered Sep 1, 2009 at 10:55

ghostdog74ghostdog74

313k55 gold badges252 silver badges339 bronze badges

2

array_pop[explode["/", "//vimeo.com/1234567"]]; will return the last element of the example url

answered Sep 1, 2009 at 10:46

nikc.orgnikc.org

16k6 gold badges47 silver badges83 bronze badges

1

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 = '//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]]];

answered Jan 10, 2017 at 14:35

Eaten by a GrueEaten by a Grue

19.9k10 gold badges79 silver badges99 bronze badges

1

answered Aug 28, 2021 at 1:11

bradbrad

1,19914 silver badges31 bronze badges

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

answered May 27, 2015 at 12:34

Mahmoud ZaltMahmoud Zalt

28.9k7 gold badges81 silver badges81 bronze badges

How to get value after last slash?

To get the value of a string after the last slash, call the substring[] method, passing it the index, after the last index of a / character as a parameter. The substring method returns a new string, containing the specified part of the original string. Copied!

How can I get the last segment of a string in PHP?

strrchr returns the portion of the string after and including the given char, not a numeric index. So you would want to do $last_section = substr[strrchr[$string, '. '], 1]; to get everything after the char.

How to get last word from url in php?

Linked.
150. ... .
Get last parameter of url in php..
Get the value of a URL after the last slash..
Get last word in URL and delete what is not necessary..
How to display different data on one php page without using the GET method..
Have app.yaml convert directory path to php script parameter..
Get last parameter from the URL..

How to get string after last slash c#?

5 Answers.
Since C# 8.0 you can also use the range operator. C# Console. WriteLine[path[pos..]]; For reference, see: docs.microsoft.com/en-us/dotnet/csharp/language-reference/… ... .
Good thing to notice how this works when there is no slash in the string. It returns the whole string, which is usually correct..

Chủ Đề