我有一个小问题;在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
感谢您的任何建议!
答案 0 :(得分:9)
使用pathinfo
答案 1 :(得分:6)
有时url会附加额外的参数。在这种情况下,我们可以首先删除参数部分,然后我们可以使用PHP的内置pathinfo()函数从URL获取图像名称。
$url = 'http://images.fitnessmagazine.mdpcdn.com/sites/story/shutterstock_65560759.jpg?itok=b8HiA95H';
检查图像网址是否附加了参数。
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作为输出。
答案 2 :(得分:5)
考虑以下是图像路径 $ 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
答案 3 :(得分:4)
$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 :(得分:1)
$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;
}
答案 5 :(得分:0)
您可以使用正则表达式/(?:.+\/)(.+\.(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
答案 6 :(得分:0)
如果您想忽略uri参数而从url获取文件名,则可以尝试以下操作:
$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
)
答案 7 :(得分:-1)
$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);
}
答案 8 :(得分:-1)
$url ='http://hhhhhh.com/content/thees/Irvine/images/hdahdahat.png';
$img = explode(".", basename($url));
echo $img['0'];
答案 9 :(得分:-1)
使用PHP pathinfo的其他人我有另一种可能对其他人有用的方法。请参见下面的示例。
/* 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, i.e. 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 i.e. '-', '_' etc */
$image_name_wo_sc = str_replace(array('_', '-'), ' ', $image_name_wo_ext);
// returns 'IMAGE Name Example'