我有一组字符串,如下所示:
1024 x 768
1280 x 960
1280 x 1024
1280 x 800 widescreen
1440 x 900 widescreen
1680 x 1050 widescreen如何发现它的最大分辨率?我说的最大,是指最高的高度和最长的宽度。在上面的例子中,1680 x 1050是最大的,因为它有最高的维度,我们可以从它创建所有其他的分辨率。
我解决这一问题的行动计划是取出分辨率值,但我只是简单的正则表达式,它不应该提取出来。然后我不知道如何用高度和宽度来确定最大分辨率尺寸。
发布于 2013-08-10 13:51:12
收集数组中的字符串,如下所示
$resolutions = array(
'1024 x 768',
'1680 x 1050 widescreen',
'1280 x 960',
'1280 x 1024',
'1280 x 800 widescreen',
'1440 x 900 widescreen'
);您可以使用sscanf从字符串中提取宽度和高度。您需要乘以宽度和高度来确定哪种分辨率具有最多的像素/是最大的分辨率。
$getPixels = function($str) {
list($width, $height) = sscanf($str, '%d x %d');
return $width * $height;
};那么要么使用array_reduce
echo array_reduce(
$resolutions,
function($highest, $current) use ($getPixels) {
return $getPixels($highest) > $getPixels($current)
? $highest
: $current;
}
);或usort数组
usort(
$resolutions,
function($highest, $current) use ($getPixels) {
return $getPixels($highest) - $getPixels($current);
}
);
echo end($resolutions);获得最高分辨率的1680 x 1050宽屏
发布于 2013-08-10 13:50:09
你只需要把宽度乘以高度就可以找到分辨率。
请注意,列表中可能没有同时具有最大宽度和最大高度的项。
PHP提取:
// I assume your set of string is an array
$input = <<<RES
1024 x 768
1280 x 960
1280 x 1024
1680 x 1050 widescreen
1280 x 800 widescreen
1440 x 900 widescreen
RES;
$resolutions = explode( "\n", $input );
// Build a resolution name / resolution map
$areas = array();
foreach( $resolutions as $resolutionName )
{
preg_match( '/([0-9]+) x ([0-9]+)/', $resolutionName, $matches );
// Affect pixel amount to each resolution string
$areas[$resolutionName] = $matches[1]*$matches[2];
}
// Sort on pixel amount
asort($areas);
// Pick the last item key
$largest = end( array_keys($areas) );
echo $largest;发布于 2013-08-10 13:53:27
您可以使用此代码获得最大分辨率:
$resolutions = array(
"1024 x 768",
"1280 x 960",
"1280 x 1024",
"1280 x 800 widescreen",
"1440 x 900 widescreen",
"1680 x 1050 widescreen"
);
$big = 0;
$max = 0;
foreach($resolution as $res){
$sides = explode(' ', $res);
if(($sides[0] * $sides[2]) > $big)
$max = $res;
}或者,如果只想保存max分辨率的索引,则可以将代码更改为:
$big = 0;
$max = 0;
$i = 0;
foreach($resolution as $res){
$sides = explode(' ', $res);
if(($sides[0] * $sides[2]) > $big)
$max = $i;
$i++;
}https://stackoverflow.com/questions/18162082
复制相似问题