9写一个函数,尽可能高效的,从一个标准 url 里取出文件的扩展名 例如: http://www.sina.com.cn/abc/de/fg.php?i
解析:<pre ><code class=答案1: function getExt($url){ $arr = parse_url($url); $file = basename($arr['path']); $ext = explode(".",$file); return $ext[1]; } 答案2: function getExt($url) { $url = basename($url); $pos1 = strpos($url,"."); $pos2 = strpos($url,"?"); if(strstr($url,"?")){ return substr($url,$pos1 + 1,$pos2 - $pos1 - 1); } else { return substr($url,$pos1); } }"php hljs">答案1: function getExt($url){ $arr = parse_url($url); $file = basename($arr['path']); $ext = explode(".",$file); return $ext[1]; } 答案2: function getExt($url) { $url = basename($url); $pos1 = strpos($url,"."); $pos2 = strpos($url,"?"); if(strstr($url,"?")){ return substr($url,$pos1 + 1,$pos2 - $pos1 - 1); } else { return substr($url,$pos1); } }</code></pre>
点击加载更多评论>>