Home >Backend Development >PHP Tutorial >Get URL file name suffix_PHP tutorial
As efficiently as possible, extract the file extension from a standard URL, and then expand the code to get other data, such as directory path. The principle is to use PHP's explode function to separate strings.
For example: http://www.abc.com/abc/de/fg.php?id=1 You need to take out php or .php
It’s very simple, just look at the code.
//This is written by myself
function getUrl($url) {
$date = explode('?', $url);
$date = basename($date[0]) ;
$date = explode('.', $date);
return $date[1];
}
var_dump(getUrl($url));
//The following two are obtained online
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");
?>