代码之家  ›  专栏  ›  技术社区  ›  Josh

来自多个文件的相同元素DomDocument loadHTMLFile PHP

  •  0
  • Josh  · 技术社区  · 16 年前

    似乎当我在“/var/www/cal/attach/”目录中有多个文件时,它只会一次又一次地从第一个文件中提取元素。我是否需要以某种方式清除元素以使其正常工作? 我试图做的是让脚本遍历多个*.htm文件,并将这些文件中的数据解析为$value[x],以便稍后调用。但多个文件显示相同的值。。。出什么事了?

    <?php
    error_reporting(0);
    
    $today = date("Y-m-d");
    
    foreach (glob("/var/www/cal/attach/*.htm") as $filename) {
    $file = $DOCUMENT_ROOT. "$filename";
    $doc = new DOMDocument();
    $doc->loadHTMLFile($file);
    
    
    $elements = $doc->getElementsByTagName('td');
    if (!is_null($elements)) {
      foreach ($elements as $element) {
        $nodes = $element->childNodes;
        foreach ($nodes as $node) {
            $value[] = $node->nodeValue. "\n";
        }
      }
    }
    echo "date,test,response time,availability,$filename\n";
    echo $today . "," . trim($value[25]) . "," . trim($value[26]) . "," . trim($value[27]) . "\n";
    echo $today . "," . trim($value[31]) . "," . trim($value[32]) . "," . trim($value[33]) . "\n";
    echo $today . "," . trim($value[37]) . "," . trim($value[38]) . "," . trim($value[39]) . "\n";
    
    }
    
    ?>
    
    1 回复  |  直到 16 年前
        1
  •  0
  •   Joshua K    16 年前

    我建议使用SPL中的目录迭代器对象。下面的示例使用递归的,但您不必是递归的。

    $dir_iterator = new RecursiveDirectoryIterator("/path");
    $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
    // could use CHILD_FIRST if you so wish
    
    
    $size = 0;
    foreach ($iterator as $file) {
        if ($file->isFile()) {
            echo substr($file->getPathname(), 27) . ": " . $file->getSize() . " B;        modified " . date("Y-m-d", $file->getMTime()) . "\n";
             $size += $file->getSize();
        }
    }
    
     echo "\nTotal file size: ", $size, " bytes\n";
    
    推荐文章