php 如何从字符串中删除扩展名(只有真正的扩展名!)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2395882/
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
How to remove extension from string (only real extension!)
提问by marc
I'm looking for a small function that allows me to remove the extension from a filename.
我正在寻找一个小函数,它允许我从文件名中删除扩展名。
I've found many examples by googling, but they are bad, because they just remove part of the string with "." . They use dot for limiter and just cut string.
我通过谷歌搜索找到了很多例子,但它们很糟糕,因为它们只是用“。”删除了部分字符串。. 他们使用点作为限制器,只是剪断字符串。
Look at these scripts,
看看这些剧本
$from = preg_replace('/\.[^.]+$/','',$from);
or
或者
$from=substr($from, 0, (strlen ($from)) - (strlen (strrchr($filename,'.'))));
When we add the string like this:
当我们像这样添加字符串时:
This.is example of somestring
这是一些字符串的例子
It will return only "This"...
它只会返回“这个”......
The extension can have 3 or 4 characters, so we have to check if dot is on 4 or 5 position, and then remove it.
扩展名可以有 3 或 4 个字符,因此我们必须检查点是否在 4 或 5 位置,然后将其删除。
How can it be done?
怎么做到呢?
回答by Timo Huovinen
http://php.net/manual/en/function.pathinfo.php
http://php.net/manual/en/function.pathinfo.php
$filename = pathinfo('filename.md.txt', PATHINFO_FILENAME); // returns 'filename.md'
回答by nickf
Try this one:
试试这个:
$withoutExt = preg_replace('/\.[^.\s]{3,4}$/', '', $filename);
So, this matches a dot followed by three or four characters which are not a dot or a space. The "3 or 4" rule should probably be relaxed, since there are plenty of file extensions which are shorter or longer.
因此,这匹配一个点后跟三个或四个不是点或空格的字符。“3 或 4”规则可能应该放宽,因为有很多文件扩展名或长或短。
回答by Erik
From the manual, pathinfo:
从手册中,pathinfo:
<?php
$path_parts = pathinfo('/www/htdocs/index.html');
echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // Since PHP 5.2.0
?>
It doesn't have to be a complete path to operate properly. It will just as happily parse file.jpgas /path/to/my/file.jpg.
它不必是正确运行的完整路径。它会file.jpg像/path/to/my/file.jpg.
回答by Lamy
Use PHP basename()
(PHP 4, PHP 5)
(PHP 4, PHP 5)
var_dump(basename('test.php', '.php'));
var_dump(basename('test.php', '.php'));
Outputs: string(4) "test"
输出:字符串(4)“测试”
回答by wprenison
This is a rather easy solution and will work no matter how long the extension or how many dots or other characters are in the string.
这是一个相当简单的解决方案,无论扩展名有多长或字符串中有多少个点或其他字符,它都将起作用。
$filename = "abc.def.jpg";
$newFileName = substr($filename, 0 , (strrpos($filename, ".")));
//$newFileName will now be abc.def
Basically this just looks for the last occurrence of . and then uses substring to retrieve all the characters up to that point.
基本上这只是寻找最后一次出现的 . 然后使用 substring 检索到该点的所有字符。
It's similar to one of your googled examples but simpler, faster and easier than regular expressions and the other examples. Well imo anyway. Hope it helps someone.
它类似于您在 google 上搜索的示例之一,但比正则表达式和其他示例更简单、更快、更容易。好吧,无论如何。希望它可以帮助某人。
回答by alex
You could use what PHP has built in to assist...
您可以使用 PHP 内置的功能来帮助...
$withoutExt = pathinfo($path, PATHINFO_DIRNAME) . '/' . pathinfo($path, PATHINFO_FILENAME);
Though if you are only dealing with a filename (.somefile.jpg), you will get...
虽然如果你只处理一个文件名 ( .somefile.jpg),你会得到...
./somefile
./某个文件
Or use a regex...
或者使用正则表达式...
$withoutExt = preg_replace('/\.' . preg_quote(pathinfo($path, PATHINFO_EXTENSION), '/') . '$/', '', $path);
If you don't have a path, but just a filename, this will work and be much terser...
如果您没有路径,而只有文件名,这将起作用并且更简洁......
$withoutExt = pathinfo($path, PATHINFO_FILENAME);
Of course, these both just look for the last period (.).
当然,这两个都只查找最后一个句点 ( .)。
回答by Emanegux
The following code works well for me, and it's pretty short. It just breaks the file up into an array delimited by dots, deletes the last element (which is hypothetically the extension), and reforms the array with the dots again.
以下代码对我来说效果很好,而且很短。它只是将文件分解为由点分隔的数组,删除最后一个元素(假设是扩展名),然后再次用点重新构建数组。
$filebroken = explode( '.', $filename);
$extension = array_pop($filebroken);
$fileTypeless = implode('.', $filebroken);
回答by bobince
I found many examples on the Google but there are bad because just remove part of string with "."
我在谷歌上找到了很多例子,但也有不好的,因为只需删除带有“.”的部分字符串。
Actually that is absolutely the correct thing to do. Go ahead and use that.
事实上,这绝对是正确的做法。继续使用它。
The file extension is everything after the last dot, and there is no requirement for a file extension to be any particular number of characters. Even talking only about Windows, it already comes with file extensions that don't fit 3-4 characters, such as eg. .manifest.
文件扩展名是最后一个点之后的所有内容,文件扩展名没有要求是任何特定数量的字符。即使只谈论 Windows,它也已经带有不适合 3-4 个字符的文件扩展名,例如。.manifest.
回答by Alex
Recommend use:pathinfowithPATHINFO_FILENAME
推荐使用:pathinfo搭配PATHINFO_FILENAME
$filename = 'abc_123_filename.html';
$without_extension = pathinfo($filename, PATHINFO_FILENAME);
回答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 havent 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'];
// Or in PHP 5.4, i believe this should work
$name = pathinfo( $filename )[ 'filename' ];
In all of these, $namecontains the filename without the extension
在所有这些中,$name包含没有扩展名的文件名

