需要关于重新关联数组值与原始键的建议
我正在尝试编写一个程序,该程序将从网页中抓取一堆图像并找出哪些图像是最大的。需要关于重新关联数组值与原始键的建议
到目前为止,我已经拍摄了图像,将它们放入数组中,使用getimagesize()函数来确定高度。然后我把所有的高度放到另一个数组中,并按相反的顺序排列它们以获得最大的数组。到现在为止还挺好。
我现在的问题是,我必须找到一种方法来重新关联最大的图像与其初始图像链接。我想过可能会运行最初的代码来从网页上再次获取图像。然后比较我用来确定最大图像的阵列中第一个值和第二个图像,但这看起来像是浪费了带宽,我感觉有一种更简单的方法将高度值与它的初始图像。我对吗?
<?php
$url = 'http://lockerz.com/s/104049300';
// Fetch page
$string = FetchPage($url);
// Regex that extracts the images (full tag)
$image_regex_src_url = '/<img[^>]*'.
'src=[\"|\'](.*)[\"|\']/Ui';
preg_match_all($image_regex_src_url, $string, $out, PREG_PATTERN_ORDER);
$img_tag_array = $out[0];
echo "<pre>"; print_r($img_tag_array); echo "</pre>";
// Regex for SRC Value
$image_regex_src_url = '/<img[^>]*'.
'src=[\"|\'](.*)[\"|\']/Ui';
preg_match_all($image_regex_src_url, $string, $out, PREG_PATTERN_ORDER);
$images_url_array = $out[1];
$image_heights_array = array();
foreach ($images_url_array as $imagelink)
{
if (substr($imagelink,0,7)=="http://")
{
$getheight = getimagesize($imagelink);
array_push($image_heights_array,"$getheight[1]");
}
}
rsort($image_heights_array);
echo "<pre>"; print_r($image_heights_array); echo "</pre>";
// Fetch Page Function
function FetchPage($path)
{
$file = fopen($path, "r");
if (!$file)
{
exit("The was a connection error!");
}
$data = '';
while (!feof($file))
{
// Extract the data from the file/url
$data .= fgets($file, 1024);
}
return $data;
}
?>
第一关:
// Regex that extracts the images (full tag)
$image_regex_src_url = '/<img[^>]*'.
'src=[\"|\'](.*)[\"|\']/Ui';
preg_match_all($image_regex_src_url, $string, $out, PREG_PATTERN_ORDER);
$img_tag_array = $out[0];
echo "<pre>"; print_r($img_tag_array); echo "</pre>";
// Regex for SRC Value
$image_regex_src_url = '/<img[^>]*'.
'src=[\"|\'](.*)[\"|\']/Ui';
preg_match_all($image_regex_src_url, $string, $out, PREG_PATTERN_ORDER);
HTML解析正则表达式EWWW。我们用一个HTML解析器来简化这个...
<?php
// EDIT: Use a custom function to do the
// reverse of SORT_NUMERIC with asort
function height_compare($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
}
$url = 'http://lockerz.com/s/104049300';
$doc = new DOMDocument();
// Okay this is HTML is kind of screwy
// So we're going to supress errors
@$doc->loadHTMLFile($url);
// Get all images
$images_list = $doc->getElementsByTagName('img');
$images = array();
foreach($images_list as $image) {
// Get the src attribute
$image_source = $image->getAttribute('src');
$image_size_info = getimagesize($image_source);
$images[$image_source] = $image_size_info[1];
}
// Do a numeric sort on the height
uasort($images, "height_compare");
print_r($images);
?>
更短,更具可读性。结果为:
$ php test.php
Array
(
[http://c0013784.cdn1.cloudfiles.rackspacecloud.com/x2_633aa94] => 328
[http://ad.doubleclick.net/ad/buz.plixi/photos;pos=300a;celeb=;kw=;tile=2;sz=300x250,300x600;ord=123456789?] => 250
[http://static.lockerz.com/pegasus/images/video_thumb.jpg?1.0.0] => 207
[http://static.lockerz.com/pegasus/images/plixi-banner.png?1.0.0] => 107
[http://ad.doubleclick.net/ad/buz.plixi/photos;pos=728a;celeb=;kw=;tile=1;sz=728x90;ord=123456789?] => 90
[http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_636f30c] => 79
[http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_637676e] => 79
[http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_63735a0] => 79
[http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_636e73c] => 79
[http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_63795d0] => 79
[http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_636a2c7] => 79
[http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_636bf79] => 79
[http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_636ca08] => 79
[http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_636e419] => 79
[http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_636deca] => 79
[http://c0013787.cdn1.cloudfiles.rackspacecloud.com/x2_6384277] => 79
我的天啊!你是个天才!感谢您的帮助! – 2011-05-24 15:22:29
@edmund查看更新的代码。我使用了一个自定义比较函数,它颠倒了顺序,所以最高的图像是第一个,而在最后使用那个令人讨厌的'array_reverse'的原始图像。 – 2011-05-24 15:30:31
太棒了!我唯一的问题是,即时通讯无法弄清楚如何获得数组中的第一个图像位置的值,我通常只会echo $ array [0],但这种方法似乎不给这个值? – 2011-05-24 15:34:10
将大小和URL保留在一个数组中,并使用usort对多维数组进行排序。
$images_url_array = $out[1];
$images = array();
foreach ($images_url_array as $imagelink)
{
if (substr($imagelink, 0, 7)=="http://")
{
$getheight = getimagesize($imagelink);
$images[] = array('height' => $getheight[1], 'url' => $imagelink);
}
}
usort($images, function ($a, $b)
{
if ($a['height'] > $b['height']) return 1;
elseif ($a['height'] < $b['height']) return -1;
else return 0;
});
好主意!我需要问另一个问题,但是,是否有一个array_push()风格的函数,自动构建一个多维数组? – 2011-05-24 14:51:22
@edmund试试这个代码。 – 2011-05-24 14:52:48
我仍然不是一个PHP的专家,所以我不得不问一下,我应该怎么试着从usort函数中回显出来,以便我能够了解它返回的值? – 2011-05-24 15:01:26
我不知道PHP,所以我不能帮助你的代码,但也许你可以尝试这个想法。让第一个数组存储一个ImageObject。这个类有三个属性,imageize,id和src。
你可以通过第一个数组,找到图像大小,然后按最大尺寸进行排序。使用id来获取单个对象。
如果使用arsort()这个数组键将被保存下来,那么你可以用reset()这个数组指针来开始数组,并获得第一个key()。
但你还需要保持整个images_url_array相同的密钥和image_height_array
foreach ($images_url_array as $key => $imagelink)
{
if (substr($imagelink,0,7)=="http://")
{
$getheight = getimagesize($imagelink);
$image_heights_array[$key] = $getheight[1];
}
}
//...
rsort($image_heights_array);
reset($image_heights_array);
$largest_image_key = key($image_heights_array);
$largest_image_url = $images_url_array[$key];
你的句子太长了。请使用句号和大写字母。 – 2011-05-24 14:45:20
@Tomalak Geret'kal请学习阅读更快,屏住呼吸更长的时间:-P – martynthewolf 2011-05-24 14:52:30
@martswite:Heh:P – 2011-05-24 15:15:36