如何从图片网址获取图片名称和图片扩展名

时间:2018-08-28 09:56:56

标签: php

我有一个带有查询字符串的图像URL。我只想在php中获取图片名称及其扩展名。

https://static01.nyt.com/images/2018/08/28/us/28vote_print/28vote_xp-articleLarge.jpg?quality=75&auto=webp&disable=upscale

4 个答案:

答案 0 :(得分:3)

您可以使用pathinfo()parse_url()

$url = 'https://static01.nyt.com/images/2018/08/28/us/28vote_print/28vote_xp-articleLarge.jpg?quality=75&auto=webp&disable=upscale';

// Getting the name
$name = pathinfo(parse_url($url)['path'], PATHINFO_FILENAME);

// Getting the extension
$ext = pathinfo(parse_url($url)['path'], PATHINFO_EXTENSION);

// Output:
var_dump($name);    // 28vote_xp-articleLarge
var_dump($ext);     // jpg

答案 1 :(得分:1)

使用php pathinfo()函数获取详细信息   http://www.php.net/manual/en/function.pathinfo.php

$fileParts = pathinfo("https://static01.nyt.com/images/2018/08/28/us/28vote_print/28vote_xp-articleLarge.jpg?quality=75&auto=webp&disable=upscale");
echo"<pre>";    
print_r($fileParts);
$extension=explode("?",$fileParts ['extension'])[0];

输出

Array
(
    [dirname] => https://static01.nyt.com/images/2018/08/28/us/28vote_print
    [basename] => 28vote_xp-articleLarge.jpg?quality=75&auto=webp&disable=upscale
    [extension] => jpg?quality=75&auto=webp&disable=upscale
    [filename] => 28vote_xp-articleLarge
)

答案 2 :(得分:0)

引用:

http://php.net/manual/en/function.parse-url.php

http://php.net/manual/en/function.pathinfo.php

<?php

$parsedURL = parse_url('https://static01.nyt.com/images/2018/08/28/us/28vote_print/28vote_xp-articleLarge.jpg?quality=75&auto=webp&disable=upscale');

$pathinfo = pathinfo($parsedURL['path']);

$filename = $pathinfo['filename'];
$extension = $pathinfo['extension'];

var_dump($filename, $extension);

检查输出:https://3v4l.org/vejBA

答案 3 :(得分:-3)

我认为您的样子如下

$explode_url=explode(".",$img_url);
        $end_typ = end($explode_url);
        $get_imgtyp=explode("?",$end_typ);
        $new_imgName=md5(time().'image').".".$get_imgtyp[0];