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
(请享用)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…