php 从文件中删除扩展名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14204727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:51:05  来源:igfitidea点击:

remove extension from file

phpfunctionfilenames

提问by user1848938

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.

我是 php 的新手,有很多东西要学!我正在为我们的 King County Iris Society 网站试用 MiniGal Nano。它非常适合我们的目的,但有一个小例外:照片文件名需要在缩略图下可见。我已经创建了一个解决方法,但它显示了扩展名。我找到了函数的代码示例,但不知道如何将它们合并到现有代码中。任何帮助将不胜感激。

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

示例链接:http: //www.kcis.org/kcisphotogallery.php?dir=Iris.Japanese

Many thanks!

非常感谢!

回答by Ascherer

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, $namecontains the filename without the extension

在所有这些中,$name包含没有扩展名的文件名

回答by hek2mgl

You can use pathinfo()for that.

你可以用pathinfo()它。

<?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'

?>

回答by rickdenhaan

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:

如果您确定文件扩展名总是四个字符长(例如“.jpg”),您可以简单地使用substr()输出文件名的位置:

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:

如果有可能出现文件扩展名中包含更多或更少字符的图像(例如“.jpeg”),您需要找出最后一个句点的位置。由于您是从第一个字符输出文件名,因此该句点的位置可用于指示要显示的字符数:

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

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

有关这些函数的信息,请查看http://php.net/substrhttp://php.net/strrpos 上的 PHP 手册。