• 09-07-2019, 00:56:20
    #1
    Merhabalar,

    Elimde satır satır verilerin olduğu bir log txt dosyası var. Ben bu dosya içerisinde istediğim örneğin istediğim IP'yi yazarak aratmak ve eğer varsa o satırları listelemek istiyorum. Yardımcı olabilir misiniz ?
  • 09-07-2019, 01:01:10
    #2
    Şurada doğrulanmış cevap mevcut:
    https://stackoverflow.com/questions/...the-whole-line
  • 09-07-2019, 01:04:13
    #3
    Php kodlarımız
    <?php
    $file = 'txt.txt';
    $find = '127.0.0.3';
    $contents = file_get_contents($file);
    $pattern = preg_quote($find, '/');
    $pattern = "/^.*$pattern.*$/m";
    if(preg_match_all($pattern, $contents, $matches)){
    echo "Sonuç bulundu";
    //echo implode("n", $matches[0]);
    }
    else{
    echo "Sonuç bulunamadı";
    } ?>
    Buda txt.txt Dosyamız.
    127.0.0.1
    127.0.0.2
    127.0.0.3
  • 09-07-2019, 01:06:14
    #4
    O kodu denedim fakat elimdeki .txt verisi 1gb büyüklüğünde aşağıdaki hatayı verdi.

    Fatal error</b>: Out of memory (allocated 262144) (tried to allocate 717628677 bytes)
  • 09-07-2019, 01:10:07
    #5
    scrol adlı üyeden alıntı: mesajı görüntüle
    O kodu denedim fakat elimdeki .txt verisi 1gb büyüklüğünde aşağıdaki hatayı verdi.

    Fatal error</b>: Out of memory (allocated 262144) (tried to allocate 717628677 bytes)
    Ozaman file_get_contents ile olmaz hafızayı tüketir fopen veya fgets ile veriyi çekeceksin.
  • 09-07-2019, 01:14:10
    #6
    XAWeb adlı üyeden alıntı: mesajı görüntüle
    Ozaman file_get_contents ile olmaz hafızayı tüketir fopen veya fgets ile veriyi çekeceksin.
    Teşekkürler, fopen ile hallettim. Örnek kod aşağıda

    <?php
    $searchthis = "mystring";
    $matches = array();
    
    $handle = @fopen("path/to/inputfile.txt", "r");
    if ($handle)
    {
        while (!feof($handle))
        {
            $buffer = fgets($handle);
            if(strpos($buffer, $searchthis) !== FALSE)
                $matches[] = $buffer;
        }
        fclose($handle);
    }
    
    //show results:
    print_r($matches);
    ?>
  • 09-07-2019, 01:16:03
    #7
    scrol adlı üyeden alıntı: mesajı görüntüle
    Teşekkürler, fopen ile hallettim. Örnek kod aşağıda

    <?php
    $searchthis = "mystring";
    $matches = array();
    
    $handle = @fopen("path/to/inputfile.txt", "r");
    if ($handle)
    {
        while (!feof($handle))
        {
            $buffer = fgets($handle);
            if(strpos($buffer, $searchthis) !== FALSE)
                $matches[] = $buffer;
        }
        fclose($handle);
    }
    
    //show results:
    print_r($matches);
    ?>
    Satırları döngüye sok mysql'e aktar istersen mariadb ile hızlı sonuç alırsın.. Ellerine sağlık iyi çalışmalar
  • 09-07-2019, 10:46:58
    #8
    Böyle büyük bir veriyi text dosyasında saklamak yerine veritabanında saklamak daha verimli olacaktır. Bunun dışında eğer linux sunucu üzerinde çalışıyorsanız ve komut çalıştırma izniniz de varsa aşağıdaki kod da size yardımcı olabilir.
    cat dosyaismi.txt | grep "aradığınız kelime"