写一个函数,尽可能高效的,从一个标准url里取出文件的扩展名,例如:http://www.sina.com.cn/abc/de/fg.php?id=1需要取出php或.php

方法一:

$url = "http://www.sina.com.cn/abc/de/fg.php?id=1";
public function getExt($url){
        $arr = parse_url($url);
        //arr = Array ( [scheme] => http [host] => www.sina.com.cn [path] => /abc/de/fg.php [query] => id=1 )
        $file = basename($arr['path']);
        $ext = explode('.',$file);
        return $ext[count($ext)-1];


    }

方法二:

public function getExt2($url){
        $basename = basename($url);
        $pos1 = strpos($basename,'.');
        $pos2 = strpos($basename,'?');
        if(strstr($basename,'?')){
            $ext =  substr($basename,$pos1+1,$pos2-$pos1-1);
            return $ext;
        }else{
            $ext = substr($basename,$pos1);
            return $ext;
        }

    }

你可能感兴趣的:(写一个函数,尽可能高效的,从一个标准url里取出文件的扩展名,例如:http://www.sina.com.cn/abc/de/fg.php?id=1需要取出php或.php)