我正在使用File
类来获取图片网址的扩展。
File::extension($image_path)
但我的图片网址为example.com/images/new.png?v=21212154
返回png?v=21212154
如何仅获取值png
?
答案 0 :(得分:1)
你应该这样试试:
$url = 'example.com/images/new.png?v=21212154';
$data = explode('?',$test);
$data = explode('/',$data[0]);
$filename = $data[2];
$extention = File::extension($filename);
或尝试使用以下代码。
$result = File::extension($image_path)
$url = strstr($result,"?");
echo str_replace($url,"",$result);
答案 1 :(得分:1)
strstr
会帮助您在某个符号前获取值:
$extension = 'png?v=21212154';
$real_extension = strstr($extension, '?', true);
// but if there's no '?' in `$extension`,
// `$real_extension` will be false
if ($real_extension) {
$extension = $real_extension;
}
答案 2 :(得分:0)
尝试使用pathinfo():
$extension = pathinfo($image_path, PATHINFO_EXTENSION);
或
$info = pathinfo(image_path);
$extension = $info['extension'];