文档对象模型功能可从数据库中获取链接数组,并一一处理它们

问题描述

|| 我一直在尝试对正在尝试构建的程序进行故障诊断,并且设法找出了导致此问题的原因,但是要解决该问题,我一直没有成功。 我在php中使用文档对象模型函数来从数据库中获取链接数组,并一个接一个地处理它们,但是似乎在@ $ doc-> loadHTMLFile(\“ $ alllinks \”);中,行,此函数从数据库接受数组值存在问题。 但是,令我感到困惑的是,如果我创建了一个通用的数组就可以了,但是从数据库(或其他一些方法)来看,它只是失败了。 这是有效的版本:
<?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,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,1);
$mainimg = key($tallest_image);

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

}
print_r($images);

?> 
这是失败的版本:
<?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,1);
$mainimg = key($tallest_image);

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

}

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

?>
    

解决方法

        带有:
@$doc->loadHTMLFile(\"\'$link\'\");
即使
$link
包含正确/可读的地址,结果也将类似:
@$doc->loadHTMLFile(\"\'http://...\'\");
而“ 5”不是正确的地址。 (见the6ѭ) 更改为:
@$doc->loadHTMLFile($link);
另外,请不要使用
@
来抑制错误:使用
libxml_use_internal_errors(true);
libxml_get_errors();
处理有问题的xml / html时。参见http://php.net/manual/function.libxml-use-internal-errors.php 另一个补充: 您不需要排序的东西。如果您只想要“最高的图像”,请使用:
$mainimg = array_search(max($images),$images);
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...