php 如何从 url 获取图像的名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8557070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the name of the image from url?
提问by johnITBonuc
I have a small question; In PHP I have used curl to get data from an URL:
我有一个小问题;在 PHP 中,我使用 curl 从 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:
我使用curl_getinfo()
它给了我一个数组:
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
如何在链接中获取图像的名称,[url] => http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg
例如
[image_name] : example
[image_ex] : jpg
Thanks for any suggestion!
感谢您的任何建议!
回答by Sinha
Sometime 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 附加了额外的参数。在这种情况下,我们可以先删除参数部分,然后我们可以使用 PHP 的内置 pathinfo() 函数从 url 中获取图像名称。
$url = 'http://images.fitnessmagazine.mdpcdn.com/sites/story/shutterstock_65560759.jpg?itok=b8HiA95H';
Check if the image url has parameters appended.
检查图像 url 是否附加了参数。
if (strpos($url, '?') !== false) {
$t = explode('?',$url);
$url = $t[0];
}
The resulting url variable now contain
生成的 url 变量现在包含
http://images.fitnessmagazine.mdpcdn.com/sites/story/shutterstock_65560759.jpg
http://images.fitnessmagazine.mdpcdn.com/sites/story/shutterstock_65560759.jpg
Use the pathinfo()to retrieve the desired details.
使用pathinfo()检索所需的详细信息。
$pathinfo = pathinfo($url);
echo $pathinfo['filename'].'.'.$pathinfo['extension'];
This will give shutterstock_65560759.jpg as output.
这将提供shutterstock_65560759.jpg 作为输出。
回答by Ravi Mane
consider 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 code
考虑以下是图片路径 $image_url=' http://development/rwc/wp-content/themes/Irvine/images/attorney1.png'; 从这个 url 获取带有扩展名的图像名称,使用以下函数 basename(); 看下面的代码
code:
代码:
$image_url='http://development/rwc/wp-content/themes/Irvine/images/attorney1.png';
echo basename($image_url);
output: attorney1.png
输出:律师1.png
回答by check123
$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;
回答by sunnyuff
$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;
}
回答by Khandad Niazi
$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);
}