Gets the URL filename suffix

  • 2020-10-07 18:35:28
  • OfStack

As efficiently as possible, take the file extension from 1 standard url and extend the code 1 to get other data, such as directory paths, by using PHP's explode function to separate strings.
For example: http: / / www. abc. com abc/de/fg php? id=1 needs to take out php or.php
It's very simple, just look at the code.


<?php 
$url = "http://www.abc.com/abc/de/fg.php?id=1";
// I wrote this 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 made 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");
?>


Related articles: