PHP获取文件拓展名的方法

1.用strrchar()函数,查找字符串在另一字符串中最后出现的位置,并返回该位置到字符串最后的所有字符(返回结果包括点)。即返回拓展名前    到结尾的字符,即为扩展名。注意与strchar()的区别。

  $res = strrchar($your_file_full_path,'.');

  echo $res;

2.strrchar()方法可看作strrpos()与substr()的结合。strrpos()返回字符在另一字符串中最后一此出现的位置并返回下标。substr()用于截取字符串,一般三个参数,分别依次为待处理字符串,开始位置,结束位置。其中结束位置可选,若不写则默认至最后。

  $spos = strpos($your_file_full_path,'.');

  $res = substr($your_file_full_path,$spos);

3.利用pathinfo()方法,返回文件信息。拓展名对应的参数为:PATHINFO_EXTENSION,或者填对应数字4也可。

  $res = pathinfo($your_file_full_path,PATHINFO_EXTENSION);

4.正则表达式

  $patt = '/^(.*?)+\.+(.*?)$/';

  preg_match($patt,$your_file_full_path,$res_arr);

  echo $res_arr[count($res_arr)-1];

5.用explode()方法拆分数组,其处理过程类似于正则表达式

  $res_arr = explode('.',$your_file_full_path);

  $echo $res_arr[count($res_arr)-1];

参考博客地址:https://www.cnblogs.com/xhen/p/10055646.html

猜你喜欢

转载自www.cnblogs.com/holiphy/p/13194722.html