php 获取 url 中最后一个 / 之后的字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1361741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:11:26  来源:igfitidea点击:

Get characters after last / in url

phpstring

提问by Johan

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

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

How do I do with php?

我如何处理 php?

回答by DisgruntledGoat

Very simply:

很简单:

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

strrposgets the position of the last occurrence of the slash; substrreturns everything after that position.

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



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

正如 redanimalwar 所提到的,如果没有斜线,这将无法正常工作,因为strrpos返回 false。这是一个更强大的版本:

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

回答by GZipp

$str = basename($url);

回答by Sampson

You could explodebased 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];
}

回答by Gabriel

You can use substrand strrchr:

您可以使用substrstrrchr

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

回答by ghostdog74

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

回答by nikc.org

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

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

回答by billynoah

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

回答by Mahmoud Zalt

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