相关疑难解决方法(0)

读取并解析非常大的文件的内容

我试图解析一个大小约1GB的制表符分隔文件.

在哪里运行脚本我得到:

Fatal error: Allowed memory size of 1895825408 bytes exhausted  (tried to allocate 1029206974 bytes) ...
Run Code Online (Sandbox Code Playgroud)

我的剧本目前只是:

$file = file_get_contents('allCountries.txt') ;

$file = str_replace(array("\r\n", "\t"), array("[NEW*LINE]", "[tAbul*Ator]"), $file) ;
Run Code Online (Sandbox Code Playgroud)

我已经将php.ini中的内存限制设置为-1,然后给了我:

Fatal error: Out of memory (allocated 1029963776) (tried to allocate 1029206974 bytes)
Run Code Online (Sandbox Code Playgroud)

是否有部分打开文件,然后继续下一部分,以便一次使用更少的内存?

php file file-get-contents

8
推荐指数
1
解决办法
3万
查看次数

在Php中读取文件时如何节省内存?

我有一个200kb的文件,我在多个页面中使用的,但在每个页面上我只需要1-2行该文件,所以如果我知道行号,我只能读取这些行所需的内容?

例如,如果我只需要第10行,我不想在内存中加载所有行,只是第10行.

对不起,我的英语不好!

php memory file line

7
推荐指数
2
解决办法
4350
查看次数

PHP处理大文件

我有一个1.5gig的大文件,我想做一些替换.

但是我的apache用完了内存.这项工作是否有可能逐行完成?

<?php

$myfile = fopen("./m.sql", "r") or die("Unable to open file!");

$dateiinhalt = file_get_contents("./m.sql");


$search = array("ä",
    "Ä",
    "ö",
    "Ö",
    "ü",
    "Ü",
    "€",
    "§",
    "ß",
    "latin1_general_ci",
    "CHARSET=latin1",
    "‚",
    "„",
    "‘",
    "’",
    "“",
    "â€",
    "©",
    "®");

$replace = array("ä",
    "Ä",
    "ö",
    "Ö",
    "ü",
    "Ü",
    "€",
    "§",
    "ß",
    "utf8_general_ci",
    "CHARSET=utf8",
    "‚",
    "„",
    "‘",
    "’",
    "“",
    "”",
    "©",
    "®");


$content = str_replace($search, $replace, $dateiinhalt);
file_put_contents("./m.sql", $content);

echo "done";
Run Code Online (Sandbox Code Playgroud)

谢谢你

更新的代码:

$reading = fopen('m.sql', 'r');
$writing = fopen('m.tmp', 'w');

$replaced = false;



$search …
Run Code Online (Sandbox Code Playgroud)

php

1
推荐指数
1
解决办法
94
查看次数

标签 统计

php ×3

file ×2

file-get-contents ×1

line ×1

memory ×1