How can I get the image name from the url?

I have a little question; In PHP, I used curl to retrieve data from a URL:

$url = "http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg"; 

With this, I use curl_getinfo() , which gave me an array:

 Array ( [url] => http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg [content_type] => image/jpeg [http_code] => 200 [header_size] => 496 [request_size] => 300 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 2.735 [namelookup_time] => 0.063 [connect_time] => 0.063 [pretransfer_time] => 0.063 [size_upload] => 0 [size_download] => 34739 [speed_download] => 12701 [speed_upload] => 0 [download_content_length] => 34739 [upload_content_length] => -1 [starttransfer_time] => 1.282 [redirect_time] => 0 ) 

How can I get the image name in the link [url] => http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg , for example

 [image_name] : example [image_ex] : jpg 

Thanks for any suggestion!

+8
source share
8 answers
+9
source

Sometimes additional parameters are added to the URL. In this case, we can first delete the parameter section, and then we can use the pathinfo () function built into PHP to get the image name from the URL.

 $url = 'http://images.fitnessmagazine.mdpcdn.com/sites/story/shutterstock_65560759.jpg?itok=b8HiA95H'; 

Make sure that parameters are added to the image URL.

 if (strpos($url, '?') !== false) { $t = explode('?',$url); $url = $t[0]; } 

The resulting url variable now contains

http://images.fitnessmagazine.mdpcdn.com/sites/story/shutterstock_65560759.jpg

Use pathinfo () to get the information you need.

 $pathinfo = pathinfo($url); echo $pathinfo['filename'].'.'.$pathinfo['extension']; 

This will give shutterstock_65560759.jpg as output.

+6
source

consider the following image path $ image_url = ' http: //development/rwc/wp-content/themes/Irvine/images/attorney1.png '; from this URL, to get the name of the image with the extension, use the following basename () function; see the following code

the code:

 $image_url='http://development/rwc/wp-content/themes/Irvine/images/attorney1.png'; echo basename($image_url); 

output: attorney1.png

+5
source
 $url_arr = explode ('/', $arr['url']); $ct = count($url_arr); $name = $url_arr[$ct-1]; $name_div = explode('.', $name); $ct_dot = count($name_div); $img_type = $name_div[$ct_dot -1]; echo $name . " " . $img_type; 
+4
source
 $URL = urldecode('http://www.greenbiz.com/sites/default/files/imagecache/wide_large/Woman_HORIZ.jpg?sunny=20$mal+1'); $image_name = (stristr($URL,'?',true))?stristr($URL,'?',true):$URL; $pos = strrpos($image_name,'/'); $image_name = substr($image_name,$pos+1); $extension = stristr($image_name,'.'); if($extension == '.jpg' || $extension == '.png' || $extension == '.gif' || $extension == '.jpeg'){`enter code here` print $image_name; } 
+1
source
 $imagePath = 'http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg'; $imageName = get_basename($imagePath); function get_basename($filename) { return preg_replace('/^.+[\\\\\\/]/', '', $imagePath); } 
-one
source
 $url ='http://hhhhhh.com/content/thees/Irvine/images/hdahdahat.png'; $img = explode(".", basename($url)); echo $img['0']; 
-one
source

Others use PHP pathinfo. I have a different approach that might be useful to someone else. See examples below.

 /* Image source */ $image_url = 'http://example.com/images/IMAGE_Name-Example.png'; /* Get image name with extension */ $image_name = basename($image_url); // returns 'IMAGE_Name-Example.png' /* Get image name without extension, ie remove '.png', '.jpg', '.gif' etc */ $image_name_wo_ext = substr($image_name, 0, strripos($image_name,'.')); // returns 'IMAGE_Name-Example' /* Get image name without special characters ie '-', '_' etc */ $image_name_wo_sc = str_replace(array('_', '-'), ' ', $image_name_wo_ext); // returns 'IMAGE Name Example' 
-one
source

All Articles