获取在last / in url之后的字符

时间:2022-06-13 08:08:18

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

我想在http://www.vimeo.com/1234567这样的url中获取最后/后的字符

How do I do with php?

如何使用php?

8 个解决方案

#1


187  

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.

strrpos获取斜线最后出现的位置;substr返回该位置之后的所有内容。


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

正如redanimalwar中提到的,如果没有斜杠,这将无法正常工作,因为strrpos返回false。这里有一个更健壮的版本:

$pos = strrpos($url, '/');
$id = $pos === false ? $url : substr($url, $pos + 1);

#2


31  

$str = basename($url);

#3


12  

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

#4


10  

You can use substr and strrchr:

您可以使用substr和strrchr:

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

#5


5  

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

#6


1  

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

最后爆炸(" / "," http://vimeo.com/1234567 "));将返回示例url的最后一个元素吗

#7


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.

两个一行——我怀疑第一个更快,但第二个更漂亮,与end()和array_pop()不同,您可以直接将函数的结果传递给current(),而无需生成任何通知或警告,因为它不会移动指针或修改数组。

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

#8


-1  

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

这里有一个漂亮的动态函数,我写它来删除url或路径的最后一部分。

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

#1


187  

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.

strrpos获取斜线最后出现的位置;substr返回该位置之后的所有内容。


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

正如redanimalwar中提到的,如果没有斜杠,这将无法正常工作,因为strrpos返回false。这里有一个更健壮的版本:

$pos = strrpos($url, '/');
$id = $pos === false ? $url : substr($url, $pos + 1);

#2


31  

$str = basename($url);

#3


12  

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

#4


10  

You can use substr and strrchr:

您可以使用substr和strrchr:

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

#5


5  

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

#6


1  

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

最后爆炸(" / "," http://vimeo.com/1234567 "));将返回示例url的最后一个元素吗

#7


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.

两个一行——我怀疑第一个更快,但第二个更漂亮,与end()和array_pop()不同,您可以直接将函数的结果传递给current(),而无需生成任何通知或警告,因为它不会移动指针或修改数组。

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

#8


-1  

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

这里有一个漂亮的动态函数,我写它来删除url或路径的最后一部分。

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