Get only filename from url in php without any vari

2019-01-14 20:23发布

I want to get filename without any $_GET variable values from a url in php?

My url is http://learner.com/learningphp.php?lid=1348

I only want to retrieve the learningphp.php from the url?

How to do this? Please help.

I used basename but it gives all the variable values also- learntolearn.php?lid=1348 which are in the url.

10条回答
迷人小祖宗
2楼-- · 2019-01-14 20:51

Use this function:

function getScriptName()
{
    $filename = baseName($_SERVER['REQUEST_URI']);
    $ipos = strpos($filename, "?");
    if ( !($ipos === false) )   $filename = substr($filename, 0, $ipos);
    return $filename;
}
查看更多
We Are One
3楼-- · 2019-01-14 20:56

Following steps shows total information about how to get file, file with extension, file without extension. This technique is very helpful for me. Hope it will be helpful to you too.

  $url = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png';
        $file = file_get_contents($url); // to get file
        $name = basename($url); // to get file name
        $ext = pathinfo($url, PATHINFO_EXTENSION); // to get extension
        $name2 =pathinfo($url, PATHINFO_FILENAME); //file name without extension
查看更多
聊天终结者
4楼-- · 2019-01-14 20:56
$filename = pathinfo( parse_url( $url, PHP_URL_PATH ), PATHINFO_FILENAME ); 

Use parse_url to extract the path from the URL, then pathinfo returns the filename from the path

查看更多
走好不送
5楼-- · 2019-01-14 20:58
$url = "learner.com/learningphp.php?lid=1348";
$l = parse_url($url);
print_r(stristr($l['path'], "/"));
查看更多
登录 后发表回答