不带点(句号)的文件扩展名
File extension without dot (period symbol)
我的代码
<?php
$video_thumb_large = 'some.example-file.name.png'; /** define a file name here **/
/** The line below is giving me what I need **/
$video_thumb_extension_large = substr($video_thumb_large, strrpos($video_thumb_large, '.') + 1);
echo $video_thumb_extension_large; /** Output: png **/
?>
还有其他获取文件扩展名的方法,例如 this Stack Overflow 问题有一些答案,但答案中没有我的代码。
我想知道为什么我的代码是好是坏,以及为什么或为什么不在生产站点上使用我的代码。在这种情况下最好做什么?我也可以在点上使用 explode()
并在 array()
中使用最后一部分,但这样更好吗?
获得不带点 (.) 的文件扩展名 会更好或最好吗?
亲身体验推荐,比Basename更快,explode或者用自己的Func
尝试使用默认的PHP Func its Cachable in All Opcaches..
重新编码只需替换为您的旧代码并执行
<?php
//Recoded by Ajmal PraveeN
$video_thumb_large = 'some.example-file.name.png'; /** define a file name here **/
$path_parts = pathinfo($video_thumb_large);
//out the file name and file extension without dot
echo 'File Name :'; echo $path_parts['filename']; echo '<br>';
echo 'File Extension :'; echo $path_parts['extension']; echo '<br>';
?>
我认为最好的方法是提取扩展名,然后从开头去掉句点,如下所示:
<?php
function get_file_extension($file_name) {
return substr(strrchr($file_name,'.'),1);
}
$video_thumb_large = 'some.example-file.name.png'; /** define a file name here **/
/** The line below is giving me what I need **/
$video_thumb_extension_large = get_file_extension($video_thumb_large);
echo $video_thumb_extension_large; /** Output: png **/
?>
我的代码
<?php
$video_thumb_large = 'some.example-file.name.png'; /** define a file name here **/
/** The line below is giving me what I need **/
$video_thumb_extension_large = substr($video_thumb_large, strrpos($video_thumb_large, '.') + 1);
echo $video_thumb_extension_large; /** Output: png **/
?>
还有其他获取文件扩展名的方法,例如 this Stack Overflow 问题有一些答案,但答案中没有我的代码。
我想知道为什么我的代码是好是坏,以及为什么或为什么不在生产站点上使用我的代码。在这种情况下最好做什么?我也可以在点上使用 explode()
并在 array()
中使用最后一部分,但这样更好吗?
获得不带点 (.) 的文件扩展名 会更好或最好吗?
亲身体验推荐,比Basename更快,explode或者用自己的Func
尝试使用默认的PHP Func its Cachable in All Opcaches..
重新编码只需替换为您的旧代码并执行
<?php
//Recoded by Ajmal PraveeN
$video_thumb_large = 'some.example-file.name.png'; /** define a file name here **/
$path_parts = pathinfo($video_thumb_large);
//out the file name and file extension without dot
echo 'File Name :'; echo $path_parts['filename']; echo '<br>';
echo 'File Extension :'; echo $path_parts['extension']; echo '<br>';
?>
我认为最好的方法是提取扩展名,然后从开头去掉句点,如下所示:
<?php
function get_file_extension($file_name) {
return substr(strrchr($file_name,'.'),1);
}
$video_thumb_large = 'some.example-file.name.png'; /** define a file name here **/
/** The line below is giving me what I need **/
$video_thumb_extension_large = get_file_extension($video_thumb_large);
echo $video_thumb_extension_large; /** Output: png **/
?>