1

我有一组字符串,如下所示:

1024 x 768  
1280 x 960  
1280 x 1024     
1280 x 800 widescreen   
1440 x 900 widescreen   
1680 x 1050 widescreen

如何找到它的最大分辨率?最大的意思是具有最高高度和最长宽度的那个。在上述情况下1680 x 1050是最大的,因为它具有最高的尺寸,我们可以从中创建所有其他分辨率。

我解决这个问题的行动计划是取出分辨率值,但我只是简单的正则表达式,不足以提取分辨率。然后不知道如何使用高度和宽度来确定最大分辨率尺寸。

4

3 回答 3

4

收集数组中的字符串,像这样

$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-10T13:51:12.247 回答
0

您可以使用此代码来获得最大分辨率:

$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;
}

或者,如果您只想保留最大分辨率的索引,您可以将代码更改为:

$big = 0;
$max = 0;
$i = 0;

foreach($resolution as $res){
$sides = explode(' ', $res);
if(($sides[0] * $sides[2]) > $big)
    $max = $i;
$i++;
}
于 2013-08-10T13:53:27.117 回答
0

您只需将宽度乘以高度即可找到分辨率。

请注意,您的列表中可能没有同时具有最大宽度和最大高度的项目。

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-10T13:50:09.617 回答