1

I want to search for the text Hello (example) in a TXT file whose size is 5GB+ then return the whole line.

I've tried using SplFileObject but what I know is that the line number is required to use SplFileObject, like that:

$linenumber = 2094; 
$file = new SplFileObject('myfile.txt');
$file->seek($linenumber-1);
echo $file->current();

But as previously mentioned, I want to search for a string then get the whole line, I don't know the line number.

Any help would be appreciated.

Mario
  • 1,374
  • 6
  • 22
  • 48
  • 2
    Have you tried [this](https://stackoverflow.com/a/13246630/215042) with a simple counter? – RobIII Mar 27 '19 at 14:24
  • You could read the file line by line with a loop? I think it could be useful to parallelize processes so for example one process read from top to bottom and at the same time another read from the bottom to the top. – Anthony Mar 27 '19 at 14:25
  • What is the size of the line? Do you only need the first occurrence, or all the lines containing that searched string? – programmer-man Mar 27 '19 at 14:28
  • @programmer-man - The line is about 100 characters, also there's only one occurrence for the string that I am looking for. – Mario Mar 27 '19 at 14:29
  • @AnthonyB - exactly, it will take a lot of time. – Mario Mar 27 '19 at 14:29
  • @MatrixCow08 You'll probably need to use several processes at the same time. How many lines has your file? – Anthony Mar 27 '19 at 14:31
  • @AnthonyB - is multi-threading possible in PHP? – Mario Mar 27 '19 at 14:31
  • 1
    @RobIII Solution seems like the best approach. You could do something like `exec('grep "Hello" myfile.txt', $return);` – user3783243 Mar 27 '19 at 14:31
  • How often are you going to do this? Would loading the file to a database be useful? – Nigel Ren Mar 27 '19 at 14:38

2 Answers2

2

this should work:

<?php
$needle = 'hello';
$count = 1;
$handle = fopen("inputfile.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        // process the line read.
        $pos = strpos($line, $needle);
        if ($pos !== false) {
            echo $line . PHP_EOL;
            echo "in line: ".$count . PHP_EOL;
            break;
        }
        $count++;
    }

    fclose($handle);
} else {
    // error opening the file.
}
YAMM
  • 572
  • 4
  • 9
-1

This is the answer that I can use. Thanks a lot to @user3783243

For Linux:

exec('grep "Hello" myfile.txt', $return);

For Windows:

exec('findstr "Hello" "myfile.txt"', $return);

Now $return should contain the whole line.

Unfortunately, this doesn't work if exec() and system() functions are disabled by your server administrator in the php.ini file. But for me it works fine.

If someone have a better solution I'd be glad to know it :)

Mario
  • 1,374
  • 6
  • 22
  • 48