13

Possible Duplicate:
How remove extension from string (only real extension!)

I am brand new to php and have a lot to learn! I'm experimenting with MiniGal Nano for our King County Iris Society website. It work quite well for our purposes with one small exception: the photo file name needs to be visible under the thumbnail. I've created a work around, but it shows the extension. I've found code samples of functions but have no idea how to incorporate them into the existing code. Any assistance would be greatly appreciated.

Example link: http://www.kcis.org/kcisphotogallery.php?dir=Iris.Japanese

Many thanks!

Community
  • 1
  • 1
user1848938
  • 133
  • 1
  • 1
  • 4
  • So in short, you want to remove the `.php` from the script name? – Supericy Jan 07 '13 at 21:56
  • It depends on how you define "extension." Everything after the last dot? Everything after any dot? Etc. You can use a regular expression, do some trickery with substrings, etc. But you need to define logically what you're trying to remove. Consider file names like `somefile.jpeg` or `some.file.jpg` or even just `somefile` with no "extension" at all. – David Jan 07 '13 at 21:59

3 Answers3

30

There are a few ways to do it, but i think one of the quicker ways is the following

// $filename has the file name you have under the picture
$temp = explode('.', $filename);
$ext  = array_pop($temp);
$name = implode('.', $temp);

Another solution is this. I haven't tested it, but it looks like it should work for multiple periods in a filename

$name = substr($filename, 0, (strlen($filename))-(strlen(strrchr($filename, '.'))));

Also:

$info = pathinfo($filename);
$name = $info['filename'];
$ext  = $info['extension'];

// Shorter
$name = pathinfo($file, PATHINFO_FILENAME);

// Or in PHP 5.4
$name = pathinfo($filename)['filename'];

In all of these, $name contains the filename without the extension

Ascherer
  • 8,223
  • 3
  • 42
  • 60
8

You can use pathinfo() for that.

<?php

// your file
$file = 'image.jpg';

$info = pathinfo($file);

// from PHP 5.2.0 :
$file_name = $info['filename'];

// before PHP 5.2.0 :
// $file_name =  basename($file,'.'.$info['extension']);

echo $file_name; // outputs 'image'

?>
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 4
    It is more usual to only ask for what is needed from the `pathinfo()` function: `pathinfo($file, PATHINFO_FILENAME)` – salathe Jan 07 '13 at 23:53
4

If you know for certain that the file extension is always going to be four characters long (e.g. ".jpg"), you can simply use substr() where you output the filename:

echo substr($filename, 0, -4);

If there's a chance that there will be images with more or less characters in the file extension (e.g. ".jpeg"), you will need to find out where the last period is. Since you're outputting the filename from the first character, that period's position can be used to indicate the number of characters you want to display:

$period_position = strrpos($filename, ".");
echo substr($filename, 0, $period_position);

For information about these functions, check out the PHP manual at http://php.net/substr and http://php.net/strrpos.

rickdenhaan
  • 10,857
  • 28
  • 37