How to make pathinfo() return the right extension?

陌路散爱 提交于 2019-12-08 04:32:36

问题


$path = 'abc.jpeg';
$info = pathinfo($path,PATHINFO_EXTENSION);
echo $info['extension'];

This is returning 'j' for some reason, instead of 'jpeg'

Is there anything I should do before calling pathinfo() ?


回答1:


If you pass a second argument to pathinfo, then it doesn't return an array.

You should just echo $info.

From the docs (realpath):

If options is used, this function will return a string if not all elements are requested.

Accessing $info['extension']; happens to be accessing the first character of the string array.

Thanks to Tim Cooper's comment. (int)'extension' evaluates to 0. In the documention on the String type in the section "String access and modification by character" outlines how strings can be accessed as arrays, in the note it mentions:

Non-integer types are converted to integer.



来源:https://stackoverflow.com/questions/5214366/how-to-make-pathinfo-return-the-right-extension

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!