Home  >  Article  >  php教程  >  获取URL文件名后缀

获取URL文件名后缀

WBOY
WBOYOriginal
2016-06-13 09:31:44826browse

尽可能高效的,从一个标准 url 里取出文件的扩展名,再把代码扩展一下就可以得到其它的数据了,比如:目录路径,原理就是使用PHP的explode函数分隔字符串。
例如: http://www.abc.com/abc/de/fg.php?id=1 需要取出 php 或 .php
很简单,直接看代码。

复制代码 代码如下:


$url = "http://www.abc.com/abc/de/fg.php?id=1";

//这个是自己写的
function getUrl($url) {
    $date = explode('?', $url);
    $date = basename($date[0]);
    $date = explode('.', $date);
    return $date[1];
}

var_dump(getUrl($url));

//下面两个是网上弄的
function getExt($url){
   $arr = parse_url($url);

   $file = basename($arr['path']);
   $ext = explode(".",$file);
   return $ext[1];
}

var_dump(getExt($url));

 

function getName($url) {

   $w_param = pathinfo($url);

   $str = $w_param['extension'];

   list($type, $vars) = explode('?',$str);

   return $type;

}
echo 'start3'.date("Y-m-d H:i:s");
?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn