Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

php - 如何获取(提取)PHP文件扩展名?(How do I get (extract) a file extension in PHP?)

This is a question you can read everywhere on the web with various answers:

(这是一个您可以在网上到处阅读的各种问题,并有各种答案:)

$ext = end(explode('.', $filename));
$ext = substr(strrchr($filename, '.'), 1);
$ext = substr($filename, strrpos($filename, '.') + 1);
$ext = preg_replace('/^.*.([^.]+)$/D', '$1', $filename);

$exts = split("[/\.]", $filename);
$n    = count($exts)-1;
$ext  = $exts[$n];

etc.

(等等)

However, there is always "the best way" and it should be on Stack Overflow.

(但是,总有“最佳方法”,它应该在堆栈溢出中。)

  ask by e-satis translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

People from other scripting languages always think theirs is better because they have a built-in function to do that and not PHP (I am looking at Pythonistas right now :-)).

(来自其他脚本语言的人们总是认为自己的语言更好,因为他们具有内置功能而不是PHP(我现在正在研究Pythonistas :-)。)

In fact, it does exist, but few people know it.

(实际上,它确实存在,但是很少有人知道。)

Meet pathinfo() :

(符合pathinfo() :)

$ext = pathinfo($filename, PATHINFO_EXTENSION);

This is fast and built-in.

(这是快速且内置的。)

pathinfo() can give you other information, such as canonical path, depending on the constant you pass to it.

(pathinfo()可以根据传递给它的常量为您提供其他信息,例如规范路径。)

Remember that if you want to be able to deal with non ASCII characters, you need to set the locale first.

(请记住,如果您希望能够处理非ASCII字符,则需要先设置语言环境。)

EG:

(例如:)

setlocale(LC_ALL,'en_US.UTF-8');

Also, note this doesn't take into consideration the file content or mime-type, you only get the extension.

(另外,请注意,这没有考虑文件内容或mime类型,您只能获得扩展名。)

But it's what you asked for.

(但这就是您要的。)

Lastly, note that this works only for a file path, not a URL resources path, which is covered using PARSE_URL.

(最后,请注意,这仅适用于文件路径,不适用于使用PARSE_URL覆盖的URL资源路径。)

Enjoy

(请享用)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...