Php 从分辨率集合中查找最大分辨率

Php 从分辨率集合中查找最大分辨率,php,regex,Php,Regex,我有一组字符串,如下所示: 1024 x 768 1280 x 960 1280 x 1024 1280 x 800 widescreen 1440 x 900 widescreen 1680 x 1050 widescreen 如何找到它的最大分辨率?我所说的最大是指高度最高、宽度最长的那个。在上面的例子中,1680x1050是最大的,因为它具有最高的维度,我们可以从中创建所有其他分辨率 我解决这个问题的行动计划是去掉分辨率值,但我只使用简单的正则表达式,不足以

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

1024 x 768  
1280 x 960  
1280 x 1024     
1280 x 800 widescreen   
1440 x 900 widescreen   
1680 x 1050 widescreen
如何找到它的最大分辨率?我所说的最大是指高度最高、宽度最长的那个。在上面的例子中,
1680x1050
是最大的,因为它具有最高的维度,我们可以从中创建所有其他分辨率


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

您只需将宽度乘以高度即可获得分辨率

$getPixels = function($str) {
    list($width, $height) = sscanf($str, '%d x %d');
    return $width * $height;
};
请注意,列表中可能没有同时具有“最大宽度”和“最大高度”的项目

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;
//我假设您的字符串集是一个数组

$input=您只需将宽度乘以高度即可获得分辨率

$getPixels = function($str) {
    list($width, $height) = sscanf($str, '%d x %d');
    return $width * $height;
};
请注意,列表中可能没有同时具有“最大宽度”和“最大高度”的项目

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;
//我假设您的字符串集是一个数组

$input=收集数组中的字符串,如下所示

$resolutions = array(
    '1024 x 768',
    '1680 x 1050 widescreen',
    '1280 x 960',
    '1280 x 1024',
    '1280 x 800 widescreen',
    '1440 x 900 widescreen'
);
可以使用从字符串中提取宽度和高度。您需要将宽度和高度相乘,以确定哪个分辨率的像素数最多/分辨率最大

$getPixels = function($str) {
    list($width, $height) = sscanf($str, '%d x %d');
    return $width * $height;
};
那么要么使用

还是数组

usort(
    $resolutions, 
    function($highest, $current) use ($getPixels) {
        return $getPixels($highest) - $getPixels($current);
    }
);

echo end($resolutions);

要获得最高分辨率的1680 x 1050宽屏

请收集数组中的字符串,如下所示

$resolutions = array(
    '1024 x 768',
    '1680 x 1050 widescreen',
    '1280 x 960',
    '1280 x 1024',
    '1280 x 800 widescreen',
    '1440 x 900 widescreen'
);
可以使用从字符串中提取宽度和高度。您需要将宽度和高度相乘,以确定哪个分辨率的像素数最多/分辨率最大

$getPixels = function($str) {
    list($width, $height) = sscanf($str, '%d x %d');
    return $width * $height;
};
那么要么使用

还是数组

usort(
    $resolutions, 
    function($highest, $current) use ($getPixels) {
        return $getPixels($highest) - $getPixels($current);
    }
);

echo end($resolutions);

要获得最高分辨率1680 x 1050宽屏

您可以使用以下代码获得最高分辨率:

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

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

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

你需要正则表达式来获取尺寸吗?您是否尝试过按空格分割,并获取结果数组的第一个和第三个元素,然后进行乘法。将该值存储在某个位置,然后将其与所有其他分辨率进行比较。@Jerry,是的,我这样做了,但这些值有许多变量,因此变成了垃圾代码。是否需要正则表达式来获取维度?您是否尝试过按空格分割,并获取结果数组的第一个和第三个元素,然后进行乘法。将该值存储在某个位置,然后将其与所有其他分辨率进行比较。@Jerry,是的,我这样做了,但这些值有很多变量,反而变成了垃圾代码。