views:

21

answers:

3

Hello everyone.

We have:

$path = "/home/httpd/html/index.php";
$file = basename($path);         // $file is set to "index.php"
$file = basename($path, ".php"); // $file is set to "index"
$file = 'index';                 // the result

How can we query a multiple file extensions? Instead of

$file = basename($path, ".php");

something like

$file = basename($path, ".php,gif,png,jpeg,css,js,html");

So, If $path = "/home/image.png"; $file will get value 'image'.

I've tryed to use pathinfo() and [filename], but my server doesn't support PHP 5.2.

Thanks.

+1  A: 

I think this will do

preg_match('/[\/]*([^\/]+)\.([^\.]+)$/i', $file, $match);
//$match[1] -> the result
//$match[2] -> the extension
Bang Dao
Works, but its too interesting, is it possible to use basename() with multiple extensions?
Happy
I think no. It can't.
Bang Dao
+1  A: 
$file = basename($path);
$info = pathinfo($file);
$name = basename($file,'.'.$info['extension']); // index

if you using PHP > 5.2.0

$info = pathinfo('/www/awesomepath/index.php');
$name = $info['filename']; //index
Pavel Morshenyuk
damn, this is a logical solution. I'm newbie at programming, sometimes I feel like a donkey.
Happy
If you already use pathino() why do you also need basename()? `echo pathinfo($path, PATHINFO_FILENAME);`
VolkerK
+1  A: 

http://www.php.net/manual/en/function.basename.php#89127

FractalizeR
thanks, but too big
Happy
What's the problem with size? Those are functions. Put them into separate file and include when you need.
FractalizeR