微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP foreach处理块时

我目前正在尝试处理一个较大的XML文件(1.5 gb),
目前正在大块开放

    $handle = fopen($url, "r") or die("Couldn't get handle");
        if ($handle) {
            while (!feof($handle)) {
            $chunk = fgets($handle, 4096);
            // echo each chunk
            echo $chunk;
        }
    fclose($handle);
    }

除了要回显此块之外,我想保存每一行,直到l< / file>.被发现.为此:

$handle = fopen($url, "r") or die("Couldn't get handle");
if ($handle) {
    while (!feof($handle)) {
        $chunk = fgets($handle, 4096);
        // echo '<xmp>'.$buffer.'</xmp>';
            if (strpos($fullstring,'</file>') !== false) {
                // i should have everything between <file> and </file>

                // empty the $fullstring so it can fill with chunks again
                $fullstring = '';
            } else {
                $fullstring .= $chunk;
            }

    }
    fclose($handle);
}

现在,我想在一个foreach循环中运行它.但是,与其循环查找每个循环,不如循环查找相同的< file>< / file>.对于找到的所有< file>< / file>.

如何处理每个< file> content< / file>大块加载文件时发现?

先感谢您!

解决方法:

如果需要解析大型XML文件,建议将XMLReader与DOM结合使用.使用XMLReader获取块元素节点,将其扩展为DOM并使用Xpath从块中获取详细信息.

$reader = new XMLReader;
$reader->open($file);
$dom = new DOMDocument;
$xpath = new DOMXpath($dom);

// look for the first chunk
while ($reader->read() && $reader->localName !== 'file') {
  continue;
}

// while you have an file element
while ($reader->localName === 'file') {
  $node = $reader->expand($dom);

  // $xpath->evaluate('expression', $node);
  // ...

  // move to the next chunk (next file sibling node)
  $reader->next('file');
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。