Bir alternative fgetcsv
ve yineleme gibi, aynı zamanda, örneğin, uygun satırları almak için normal ifadeleri kullanabilirsiniz için
Date,Event,Description
24/01/2010,Football,Football practice for all Years.
24/01/2010,Cricket,Cricket Practice for all Years.
25/01/2010,Piano Lessons,Paino lessons for Year 10.
26/01/2010,Piano Lessons II.
kullanmak
date_default_timezone_set('Europe/Berlin');
$pattern = sprintf('#%s.*|%s.*#', date('d/m/Y'),
date('d/m/Y', strtotime("+1 day")) );
$file = file_get_contents('filename.csv');
preg_match_all($pattern, $file, $matches);
var_dump($matches);
ve alma
array(1) {
[0]=>
array(3) {
[0]=> string(53) "24/01/2010,Football,Football practice for all Years."
[1]=> string(51) "24/01/2010,Cricket,Cricket Practice for all Years."
[2]=> string(52) "25/01/2010,Piano Lessons,Paino lessons for Year 10."
}
}
Gerçi bu Benchmarking değil. CSV dosyasının boyutuna bağlı olarak, bu nedeniyle file_get_contents
bir değişken tüm dosya yükleme için, yoğun bellek alabilir.
Başka bir alternative ile SplFileObject,
$today = date('d/m/Y');
$tomorrow = date('d/m/Y', strtotime("+1 day"));
$file = new SplFileObject("csvfile.csv");
$file->setFlags(SplFileObject::READ_CSV);
foreach ($file as $row) {
list($date, $event, $description) = $row;
if($date === $today || $date === $tomorrow) {
echo "Come visit us at $date for $description";
}
}