【发布时间】:2015-09-19 13:29:47
【问题描述】:
我目前正在尝试处理一个大型 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);
}
我不想回显这个块,而是保存每一行,直到找到</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>?
提前谢谢你!
【问题讨论】: