PHP函数获取文件扩展名

发布于 2024-11-07 23:42:27 字数 297 浏览 0 评论 0原文

任何人都可以帮我更改此脚本以使用 preg_split (php.net 推荐的替代品)而不是不再使用的 split 。此函数获取变量 $filename 中任何上传文件的文件扩展名。

function findExtension ($filename)
{
   $filename = strtolower($filename) ;
   $exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}

Can anyone help me change this script to use preg_split (recommended substitute by php.net) instead of split which is not used anymore. This function gets the file extension of any uploaded file in the variable $filename.

function findExtension ($filename)
{
   $filename = strtolower($filename) ;
   $exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

[浮城] 2024-11-14 23:42:27

您应该只使用 pathinfo 代替:

$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');

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

You should just use pathinfo instead:

$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');

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
牵你手 2024-11-14 23:42:27

你为什么不使用这个函数: http://www.php .net/manual/fr/function.finfo-file.php 或这个:https://www.php.net/manual/fr/function.pathinfo.php

你也可以使用explode

function findExtension ($filename)
{
   $filename = strtolower($filename) ;
   $exts = explode(".", $filename) ;
   $n = count($exts)-1;
   $exts = $exts[$n];
   return $exts;
}

Why don't u use this function : http://www.php.net/manual/fr/function.finfo-file.php or this one : https://www.php.net/manual/fr/function.pathinfo.php

you can also use explode

function findExtension ($filename)
{
   $filename = strtolower($filename) ;
   $exts = explode(".", $filename) ;
   $n = count($exts)-1;
   $exts = $exts[$n];
   return $exts;
}
夏末的微笑 2024-11-14 23:42:27

您可以使用 explodesplit >。由于您只需要扩展名,因此没有理由用 / 分割,只需按点分割并使用 array_pop

Instead of split you can just use explode. As you just want the extension, there's no reason to split by /, just split by the dot and get the last element with array_pop.

放飞的风筝 2024-11-14 23:42:27

我更喜欢 David Walsh 发布的函数,它使用“strrchr”函数获取最后一次出现的“.”在一个字符串中。


function get_file_extension($file_name)
{
  return substr(strrchr($file_name,'.'),1);
}

I prefer a function David Walsh posted, that uses the "strrchr" function to get the last occurrence of "." in a string.


function get_file_extension($file_name)
{
  return substr(strrchr($file_name,'.'),1);
}
残疾 2024-11-14 23:42:27

如果文件扩展名是您唯一想要的部分:

function GetExt($filename) {
    return (($pos = strrpos($filename, '.')) !== false ? substr($filename, $pos+1) : '');
}

If the file extension is the only part you want:

function GetExt($filename) {
    return (($pos = strrpos($filename, '.')) !== false ? substr($filename, $pos+1) : '');
}
软甜啾 2024-11-14 23:42:27

也许是这样的?

$string = "some/path_to_a_file.txt";
$pattern = preg_split('/\./', $string, -1, PREG_SPLIT_OFFSET_CAPTURE); 

Perhaps something along these lines?

$string = "some/path_to_a_file.txt";
$pattern = preg_split('/\./', $string, -1, PREG_SPLIT_OFFSET_CAPTURE); 
挽容 2024-11-14 23:42:27

我的代码将给出文件扩展名,删除查询字符串。 pathinfo 还返回带有字符串的扩展名。因此,如果您想知道确切的文件名,请使用我的代码:

$filename = 'http://doamin/js.jquery.min.js?v1.1.11';    
preg_replace('/\?.*/', '', substr(strrchr($filename, '.'), 1));

// output: js

My code will give file extension, removing query strings. pathinfo also return extension with string. so use my code if you want to know the exact file name:

$filename = 'http://doamin/js.jquery.min.js?v1.1.11';    
preg_replace('/\?.*/', '', substr(strrchr($filename, '.'), 1));

// output: js
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文