我如何从url获得图像的名称


How can I get the name of the image from url?

我有一个小问题;在PHP中,我使用curl从URL获取数据:

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

我使用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
)

如何获得链接[url] => http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg中的图像名称,例如

[image_name] : example
[image_ex] : jpg

谢谢你的建议!

使用pathinfo

有时候url有附加的参数。在这种情况下,我们可以先删除参数部分,然后我们可以使用PHP内置的pathinfo()函数从url中获取图像名称。

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

检查图片url是否有附加参数

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

结果url变量现在包含

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

使用pathinfo()来检索所需的详细信息。

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

这将输出shutterstock_65560759.jpg

考虑以下是图像路径美元image_url = ' http://development/rwc/wp-content/themes/Irvine/images/attorney1.png ';从这个url获取带有扩展名的图像名称,使用下面的函数basename();查看下面的代码

代码:

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

输出:attorney1.png

$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;
$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;
}

您可以使用regex /(?:.+'/)(.+'.(png|jpg|jepg))[?#]?.*$/

的例子:

$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";   
    }
}

印刷:

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 

如果你想从url中获取文件名而忽略uri参数,你可以试试:

$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
)
$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);
}