获取URL PHP的最后一部分
我只是想知道如何使用PHP提取URL的最后一部分。
示例URL是:
http://domain.com/artist/song/music-videos/song-title/9393903
现在如何使用PHP提取最后一部分?
9393903
URL中始终有相同数量的变量,并且id始终在末尾。
Belgin Fish asked 2019-12-31T23:58:59Z
12个解决方案
99 votes
绝对最简单的方法是使用2668661353583805440
echo basename('http://domain.com/artist/song/music-videos/song-title/9393903');
哪个会打印
9393903
当然,如果末尾有查询字符串,它将包含在返回值中,在这种情况下,可接受的答案是一个更好的解决方案。
nikc.org answered 2019-12-31T23:59:55Z
91 votes
将其拆分并获得最后一个元素:
$end = end(explode('/', $url));
# or:
$end = array_slice(explode('/', $url), -1)[0];
编辑:要支持apache-style-canonical URL,preg_match很方便:
$end = end(explode('/', rtrim($url, '/')));
# or:
$end = array_slice(explode('/', rtrim($url, '/')), -1)[0];
我认为可能更具可读性的另一个示例是(Demo):
$path = parse_url($url, PHP_URL_PATH);
$pathFragments = explode('/', $path);
$end = end($pathFragments);
此示例还考虑到仅在URL路径上工作。
(多年之后)还包括规范化和易于使用的UTF-8替代用法(通过PHP中的PCRE正则表达式):
use function call_user_func as f;
use UnexpectedValueException as e;
$url = 'http://example.com/artist/song/music-videos/song-title/9393903';
$result = preg_match('(([^/]*)/*$)', $url, $m)
? $m[1]
: f(function() use ($url) {throw new e("pattern on '$url'");})
;
var_dump($result); # string(7) "9393903"
这很粗糙,但显示了如何将其包装在preg_match调用中,以通过PCRE正则表达式模式进行更细粒度的控制。 为了使这个裸机示例更具意义,应该将其包装在其自身的函数内(这也将使别名变得多余)。 为了简洁起见,只介绍了这种方法。
hakre answered 2020-01-01T00:00:37Z
24 votes
您可以使用http://匹配所需的URL部分。
在这种情况下,由于模式很简单,我们正在寻找一个正斜杠(http://,由于正斜杠表示正则表达式模式的开始和结束,因此我们必须对其进行转义),以及一个或多个数字(/^http:\/\/.*\/(\d+)$/ )放在字符串的最末端(.*)。 \d+周围的括号用于捕获我们想要的片段:即结尾。 然后,将所需的结尾($end)分配给$matches[1](而不是$matches[0]),因为这与$url(即整个字符串)相同。
$url='http://domain.com/artist/song/music-videos/song-title/9393903';
if(preg_match("/\/(\d+)$/",$url,$matches))
{
$end=$matches[1];
}
else
{
//Your URL didn't match. This may or may not be a bad thing.
}
注意:您可能会或可能不想在此正则表达式中添加更多的复杂性。 例如,如果您知道URL字符串将始终以http://开头,则正则表达式可以变为/^http:\/\/.*\/(\d+)$/(其中.*表示零个或多个字符(不是换行符))。
answered 2019-12-31T23:59:22Z
15 votes
如果您正在寻找可以处理任何形式的URL的健壮版本,则应该做得很好:
$url = "http://foobar.com/foo/bar/1?baz=qux#fragment/foo";
$lastSegment = basename(parse_url($url, PHP_URL_PATH));
Peter answered 2020-01-01T00:00:57Z
12 votes
$id = strrchr($url,"/");
$id = substr($id,1,strlen($id));
这是strrchr函数的描述:[http://www.php.net/manual/zh/function.strrchr.php]
希望这是有用的!
Tamer Shlash answered 2020-01-01T00:01:22Z
4 votes
另外的选择:
$urlarray=explode("/",$url);
$end=$urlarray[count($urlarray)-1];
someone answered 2020-01-01T00:01:41Z
3 votes
最优雅的解决方案之一是在此处获取last / in url之后的字符
通过心怀不满的山羊
$id = substr($url, strrpos($url, '/') + 1);
strrpos获取最后一次出现斜线的位置; substr 返回该位置之后的所有内容。
Robert Sinclair answered 2020-01-01T00:02:10Z
2 votes
一班轮:$page_path = end(explode('/', trim($_SERVER['REQUEST_URI'], '/')));
获取URI,修剪斜线,转换为数组,获取最后一部分
Justin answered 2020-01-01T00:02:34Z
1 votes
这样可以轻松完成所需网址的最后一部分
$url="http://domain.com/artist/song/music-videos/song-title/9393903";
$requred_string= substr(strrchr($url, "/"), 1);
这将使您从右边的第一个“ /”后面得到字符串。
XxANxX answered 2020-01-01T00:02:59Z
0 votes
1线
$end = preg_replace( '%^(.+)/%', '', $url );
// if( ! $end ) no match.
这只是删除了最后一个斜杠之前的所有内容,包括它。
Jorge Orpinel answered 2020-01-01T00:03:23Z
0 votes
一线工作答案:
$url = "http://www.yoursite/one/two/three/drink";
echo $end = end((explode('/', $url)));
输出:饮料
George Chalhoub answered 2020-01-01T00:03:47Z
0 votes
$mylink = $_SERVER['PHP_SELF'];
$link_array = explode('/',$mylink);
echo $lastpart = end($link_array);
Shuhad zaman answered 2020-01-01T00:04:03Z