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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...