- This topic is empty.
- AuthorPosts
-
December 19, 2011 at 4:24 am #9927
johnitbonuc
ParticipantI have a small question; In PHP I have used curl to get data from an URL:
$url = "http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg";
With that 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 name of the image in the link
[url] => http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg
such as[image_name] : example [image_ex] : jpg
Thanks for any suggestion!
December 19, 2011 at 4:27 am #9935markel-mairs
ParticipantUse pathinfo
December 19, 2011 at 4:32 am #9932check123
Participant$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;
June 16, 2012 at 6:47 am #9931sunny
Participant$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; }
January 27, 2014 at 11:25 am #9928khandad-niazi
Participant$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); }
January 19, 2016 at 5:17 am #9933ravi-mane
Participantconsider the following is the image path
$image_url=’http://development/rwc/wp-content/themes/Irvine/images/attorney1.png‘;
from this url to get image name with extention use following function basename();
look following codecode:
$image_url='http://development/rwc/wp-content/themes/Irvine/images/attorney1.png'; echo basename($image_url);
output:
attorney1.pngApril 28, 2017 at 11:02 am #9934sinha
ParticipantSometime url has additional parameters appended to it. In such scenario we can remove the parameter section first and then we can use the PHP’s inbuilt pathinfo() function to get the image name from the url.
$url = 'http://images.fitnessmagazine.mdpcdn.com/sites/story/shutterstock_65560759.jpg?itok=b8HiA95H';
Check if the image url has parameters appended.
if (strpos($url, '?') !== false) { $t = explode('?',$url); $url = $t[0]; }
The resulting url variable now contain
http://images.fitnessmagazine.mdpcdn.com/sites/story/shutterstock_65560759.jpg
Use the pathinfo() to retrieve the desired details.
$pathinfo = pathinfo($url); echo $pathinfo['filename'].'.'.$pathinfo['extension'];
This will give shutterstock_65560759.jpg as output.
September 22, 2020 at 9:49 am #9929yaroslawww
ParticipantYou can use regex
/(?:.+\/)(.+\.(png|jpg|jepg))[?#]?.*$/
Example:
$examples = [ 'http://images.fitnessmagazine.mdpcdn.com/sites/story/with_query.jpg?itok=b8HiA95H', 'http://images.fitnessmagazine.mdpcdn.com/sites/story/with_hash.jpg#hghgh', 'http://images.fitnessmagazine.mdpcdn.com/sites/story/with_query.jpg?', 'http://images.fitnessmagazine.mdpcdn.com/sites/story/with_hash.jpg#', 'http://images.fitnessmagazine.mdpcdn.com/sites/story/with_multydots.65560759.jpg', 'http://images.fitnessmagazine.mdpcdn.com/sites/story/image.png', 'http://images.fitnessmagazine.mdpcdn.com/sites/story/without_ext', ]; foreach($examples as $example) { preg_match('/(?:.+\/)(.+\.(png|jpg|jepg))[?#]?.*$/', $example, $matches); if(isset($matches[1]) && $matches[1]) { echo "Url: {$example}, image name: {$matches[1]} \n"; } else { echo "Url: {$example} is not image url \n"; } }
Printed:
Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/with_query.jpg?itok=b8HiA95H, image name: with_query.jpg Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/with_hash.jpg#hghgh, image name: with_hash.jpg Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/with_query.jpg?, image name: with_query.jpg Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/with_hash.jpg#, image name: with_hash.jpg Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/with_multydots.65560759.jpg, image name: with_multydots.65560759.jpg Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/image.png, image name: image.png Url: http://images.fitnessmagazine.mdpcdn.com/sites/story/without_ext is not image url
October 23, 2020 at 7:18 am #9930hett
ParticipantIf you wanna to get file name from url ignoring uri params, you can try this:
$url = 'http://host/filename.ext?params'; $parsedUrl = parse_url($url); $pathInfo = pathinfo($parsedUrl['path']); print_r($pathInfo); Array ( [dirname] => / [basename] => filename.ext [extension] => ext [filename] => filename )
- AuthorPosts
- You must be logged in to reply to this topic.