PHP foreach处理块时 [英] PHP foreach while handling chunks

查看:57
本文介绍了PHP foreach处理块时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently trying to handle a large XML file (1,5 gb), Currently it's opening in chunks

    $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);
    }

我不想保存此块,而是要保存每个直到找到< / file> 。为此:

Instead of echo'ing this chunk, I'd like to save up each line untill </file> is found. therefor:

$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>

Now I'd like to run this in a foreach loop. But instead of looping each found it loops the same <file></file> for all <file></file>'s found.

如何处理每个< file> content< / file> 批量加载文件时找到了吗?

How can I process each <file>content</file> found while loading the file in chunks?

谢谢!

推荐答案

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

If you need to parse a large XML file, I suggest combining XMLReader with DOM. Use XMLReader to get the chunk element node, expand it into DOM and use Xpath to fetch the details from the chunk.

$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');
}

这篇关于PHP foreach处理块时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆