我想知道如何使用PHP提取URL的最后一部分。
示例URL是:
http://domain.example/artist/song/music-videos/song-title/9393903
现在我该如何使用PHP提取最后一部分?
9393903
URL中的变量数量始终相同,并且id始终位于末尾。
$url="http://domain.example/artist/song/music-videos/song-title/9393903";
$requred_string= substr(strrchr($url, "/"), 1);
$mylink = $_SERVER['PHP_SELF'];
$link_array = explode('/',$mylink);
echo $lastpart = end($link_array);
一行代码
$end = preg_replace( '%^(.+)/%', '', $url );
// if( ! $end ) no match.
这个简单地移除了最后一个斜杠之前的所有内容,包括它本身。
一行有效的答案:
$url = "http://www.yoursite/one/two/three/drink";
echo $end = end((explode('/', $url)));
输出:饮料