Nasıl PHP kullanarak belirtilen bir hattan txt dosyası okumaya başlamak için?

0 Cevap php

Ben sadece güncel sürümü için yeni değişiklikleri görüntülemek çalışırken bir değişim-log.I 'm var bir txt dosyası var.

Ben dosyayı okumak ve istenen kelimeler varsa bu kelimeleri bulursa o içerik almak ve bir dizi onu itmeye başlar, her satırını kontrol etmek için bir fonksiyon yazdım.

Ben bir örnek olup olmadığını görmek için aradı ama herkes birinden başlamak için değil, belirli bir çizgi durdurmak için nasıl bahsediyordu.

Burada kullandığım kod:

public function load($theFile, $beginPosition, $doubleCheck) {

    // Open file (read-only)
    $file = fopen($_SERVER['DOCUMENT_ROOT'] . '/home/' . $theFile, 'r');

    // Exit the function if the the file can't be opened
    if (!$file) {
        return;
    }

    $changes = Array();

    // While not at the End Of File
    while (!feof($file)) {

        // Read current line only
        $line = fgets($file);

        // This will check if the current line has the word we look for to start loading
        $findBeginning = strpos($line, $beginPosition);

        // Double check for the beginning
        $beginningCheck = strpos($line, $doubleCheck);

        // Once you find the beginning
        if ($findBeginning !== false && $beginningCheck !== false) {

            // Start storing the data to an array
            while (!feof($file)) {

                $line = fgets($file);

                // Remove space and the first 2 charecters ('-' + one space)
                $line = trim(substr($line, 2));

                if (!empty($line)) { // Don't add empty lines
                    array_push($changes, $line);
                }
            }
        }
    }

    // Close the file to save resourses
    fclose($file);

    return $changes;
}

Şu anda çalışıyor, ama gördüğünüz gibi bu iç içe döngüler olduğunu ve bu iyi değil ve durumda txt dosyası daha fazla zaman alacak büyür!

Ben bu yüzden bunu yapmak için daha iyi bir yolu yoktur yapar, performansını artırmak için çalışıyorum?

0 Cevap