我在处理大文件时没有经验,所以我不知道该怎么做.我试图使用file_get_contents读取几个大文件; 任务是使用preg_replace()清理和消除它们.
我的代码在小文件上运行良好; 但是,大文件(40 MB)会触发内存耗尽错误:
PHP Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 41390283 bytes)
Run Code Online (Sandbox Code Playgroud)
我正在考虑使用fread(),但我不确定它是否也能正常工作.这个问题有解决方法吗?
感谢您的输入.
这是我的代码:
<?php
error_reporting(E_ALL);
##get find() results and remove DOS carriage returns.
##The error is thrown on the next line for large files!
$myData = file_get_contents("tmp11");
$newData = str_replace("^M", "", $myData);
##cleanup Model-Manufacturer field.
$pattern = '/(Model-Manufacturer:)(\n)(\w+)/i';
$replacement = '$1$3';
$newData = preg_replace($pattern, $replacement, $newData);
##cleanup Test_Version field and create comma delimited layout. …Run Code Online (Sandbox Code Playgroud) php ×1