Reputation: 277
I have a image url with query strings. I just want to get image name and its extension in php.
https://static01.nyt.com/images/2018/08/28/us/28vote_print/28vote_xp-articleLarge.jpg?quality=75&auto=webp&disable=upscale
Upvotes: 4
Views: 1692
Reputation: 6006
use php pathinfo()
function to get the details
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];
Output
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
)
Upvotes: 1
Reputation: 7554
You can use pathinfo()
and 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
Upvotes: 3
Reputation: 2223
Refer:
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);
Check the output: https://3v4l.org/vejBA
Upvotes: 0