Get only filename from url in php without any variable values which exist in the url

前端 未结 12 695
北荒
北荒 2020-12-05 12:41

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

相关标签:
12条回答
  • 2020-12-05 13:32

    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
    
    0 讨论(0)
  • 2020-12-05 13:34

    You can use,

    $directoryURI =basename($_SERVER['SCRIPT_NAME']);
    
    echo $directoryURI;
    
    0 讨论(0)
  • 2020-12-05 13:38
    $url = "learner.com/learningphp.php?lid=1348";
    $l = parse_url($url);
    print_r(stristr($l['path'], "/"));
    
    0 讨论(0)
  • 2020-12-05 13:40

    This should work:

    echo basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['QUERY_STRING']);
    

    But beware of any malicious parts in your URL.

    0 讨论(0)
  • 2020-12-05 13:43

    An other way to get only the filename without querystring is by using parse_url and basename functions :

    $parts = parse_url("http://example.com/foo/bar/baz/file.php?a=b&c=d");
    $filename = basename($parts["path"]); // this will return 'file.php'
    
    0 讨论(0)
  • 2020-12-05 13:45
    $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

    0 讨论(0)
提交回复
热议问题