tags:

views:

55

answers:

6

Hi, what I want to do is PHP to look at the url and just grab the name of the file, without me needing to enter a path or anything (which would be dynamic anyway). E.G.

http://google.com/info/hello.php, I want to get the 'hello' bit.

Help?

Thanks.

+1  A: 
$filename = __FILE__;

Now you can split this on the dot, for example

$filenameChunks = split(".", $filename);

$nameOfFileWithoutDotPHP = $filenameChunks[0];
Snake
basename function is way better, this will fail for foo.file.php etc.
Mikulas Dite
A: 

$_SERVER['REQUEST_URI'] contains the requested URI path and query. You can then use parse_url to get the path and basename to get just the file name:

basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '.php')
Gumbo
+1  A: 

You need basename and explode to get name without extension:

$name = basename($_SERVER['REQUEST_URI']);
$name_array = explode('.', $name);
echo $name_array[0];
Sarfraz
thankyou :D! This was very helpful
Sam
@Sam: You are welcome :)
Sarfraz
Actually this is not always correct, what if you have an .htaccess which rewrites the url?
Snake
`basename` is not suitable for URIs. Consider `basename('/foo/bar?baz/quux')` returning `quux` instead of `bar`.
Gumbo
A: 

http://php.net/manual/en/function.basename.php

$file = basename(__FILE__); // hello.php
$file = explode('.',$file); // array
unset($file[count($file)-1]); // unset array key that has file extension
$file = implode('.',$file); // implode the pieces back together
echo $file; // hello
acmatos
A: 

You could to this with parse_url combined with pathinfo

Here's an example

$parseResult = parse_url('http://google.com/info/hello.php');
$result = pathinfo($parseResult['path'], PATHINFO_FILENAME);

$result will contain "hello"

More info on the functions can be found here: parse_url pathinfo

murze
+1  A: 

This is safe way to easily grab the filename without extension

$info = pathinfo(__FILE__);
$filename = $info['filename'];
Mikulas Dite