5.PHP|5.PHP YIELD使读取大文件

PHP YIELD使读取大文件

/** * [getLines 逐行读取文件中数据] * @author Jhou Shuai * @param[type] $file [description] * @return [type][description] */ public function getLines($file) { $f = fopen($file, 'r'); try { while ($line = fgets($f)) { yield $line; } } finally { fclose($f); } }foreach ($this->getLines('mytest.txt') as $n => $line) { echo $line; }/** * [_parse CSV文件读取] * @author Jhou Shuai * @datetime 2018-12-20T13:48:03+0800 */ private function _parse($filename, $keypos = 0) { $data = https://www.it610.com/article/array(); if (($handle = fopen($filename,"r")) !== FALSE) { while (($line = fgetcsv($handle, 1000, ",")) !== FALSE) { yield $line; } fclose($handle); } }$file = '/var/www/xxxx/data/xxxx.csv'; foreach ($this->_parse($file, 0) as $line) { $idcard = preg_replace('# #', '', trim($line[2])); $idcard = strtoupper($idcard); }

    推荐阅读