开发者

Document Object Model function to take an array of links from a database and process them one by one

I've been trying to troubleshoot a program I'm trying to build and I've managed to figure out what is causing the problem, however to fix it I have been unsuccessful.

I am using the document object model function in php to take an array of links from a database and process them one by one, however it appears that in the @$doc->loadHTMLFile("$alllinks"); line there is a problem with this function accepting array values from the database.

what I find puzzling however is if I create a general array it works, but from the database (or a few other methods) it simply fails.

here is the version that works:

<?php session_start();


// EDIT: Use a custom function to do the
// reverse of SORT_NUMERIC with asort
function height_compare($a1, $b1)
{
    if ($a1 == $b1) {
        return 0;
    }
    return ($a1 > $b1) ? -1 : 1;
}

$all_links = array('http://example.com/klvvobj','http://example.net/s/109228626');

foreach($all_links as $alllinks){
$doc = new DOMDocument();
// Okay this is HTML is kind of screwy
// So we're going to suppress errors
@$doc->loadHTMLFile("$alllinks");

// Get all images
$images_list = $doc->getElementsByTagName('img');

$images = array();
foreach($images_list as $image) {

  // Get the src attribute
  $image_source = $image->getAttribute('src');

  if (substr($image_source,0,7)=="http://"){
  $image_size_info = getimagesize($image_source);

  $images[$image_source] = $image_size_info[1];
  print_r($images);
  }
}


// Do a numeric sort on the height
uasort($images, "height_compare");
$tallest_image = array_slice($images, 0,1);
$mainimg = key($tallest_image);

echo "<img src='$mainimg' />";

}
print_r($images);

?> 

and here is the version that fails:

<?php session_start();

include 'functions.php';

$query = mysql_query("SELECT * FROM links");

        function height_compare($a1, $b1)
{
    if ($a1 == $b1) {
        return 0;
    }
    return ($a1 > $b1) ? -1 : 1;
}

while($result = mysql_fetch_array($query)) {
$link = $result['link'];
$doc = new DOMDocument();

// Okay this is HTML is kind of screwy
// So we're going to suppress errors
@$doc->loadHTMLFile($link);

// Get all images
$images_list = $doc->getElementsByTagNam开发者_运维百科e('img');

$images = array();
foreach($images_list as $image) {

  // Get the src attribute
  $image_source = $image->getAttribute('src');

  if (substr($image_source,0,7)=="http://"){
  $image_size_info = getimagesize($image_source);

  $images[$image_source] = $image_size_info[1];
  print_r($images);
  }
}


// Do a numeric sort on the height
uasort($images, "height_compare");
$tallest_image = array_slice($images, 0,1);
$mainimg = key($tallest_image);

echo "<img src='$mainimg' />";

}

                     echo '<a href="logout.php">Log out</a>';

?>


With:

@$doc->loadHTMLFile("'$link'");

even if $link contains a correct/readable address, the result will be something like:

@$doc->loadHTMLFile("'http://...'");

and 'http://...' is not a correct address.

(See the ')

Change it to:

@$doc->loadHTMLFile($link);


Also, please don't use the @ for error surpressing: Use

libxml_use_internal_errors(true);

and

libxml_get_errors();

when dealing with problematic xml/html. See http://php.net/manual/function.libxml-use-internal-errors.php


Another addition:

You don't need the sort stuff. If you just want the "tallest images" use:

$mainimg = array_search(max($images), $images);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜