douxiaochun4964 2011-06-16 14:03
浏览 29
已采纳

文档对象模型函数,用于从数据库中获取一系列链接并逐个处理它们

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->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' />";

}

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

?>
  • 写回答

1条回答 默认 最新

  • doubaolan2842 2011-06-16 14:10
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办