写一个函数,尽可能高效的,从一个标准 url 里取出文件的扩展名

时间:2023-03-09 14:48:18
写一个函数,尽可能高效的,从一个标准 url 里取出文件的扩展名

例如: http://www.sina.com.cn/abc/de/fg.php?id=1 需要取出 php 或 .php

function getExt($url){

$arr=parse_url($url);//var_dump得到array(4) { ["scheme"]=> string(4) "http" ["host"]=> string(15) "www.sina.com.cn" ["path"]=> string(14) "/abc/de/fg             //.php" ["query"]=> string(4) "id=1" }

$dir=basename($arr['path']);

$ext=explode(".",$dir);

echo $ext[1];

}

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

getExt($s);