使用PHP在.txt文件中搜索短语

I have got a HUGE file in which there are lines of data (pretty much of equal length). I would like to browse through them and find every occurance of a certain word, then take the whole line where that word occured and echo it out to the screen. Currently I am using this method:

$result_counter = 0;
$result_array[1] = 'No results';

$search_slug = $request->input('search_slug');
$fp = fopen(base_path('storage\app\xas.txt'), "r");

while (false !== ($line = fgets($fp))) {
    // Process $line, e.g split it into values since it is CSV.

    $exploded = explode('-', $line);
    $exploded = implode('', $exploded);
    set_time_limit(30);
    if(strpos($exploded, strtoupper($search_slug))) {
        $result_counter++;
        $result_array[$result_counter] = $exploded;
    }
}
return view('results')->with(array('results' => $result_array, 'query' => $search_slug));
fclose($fp);

Going line by line through a huge file just takes too long.

You might ask - why don't you use MySQL?

There is a huge problem with that - my file is 2.5 million lines long and might be even bigger soon. The owner of my server will ban my account if I try to update the database in one go, not to mention the fact that people are going to query large portions of it on an hourly basis. I can't afford a "no-limit" server either. I will move to MySQL as soon as it's possible, but right now I need a quick solution.

How can I solve this problem using PHP? Is there a way to search for word occurances in a file and then grabbing those lines only?

You can use a regex like this ^.*yourword.*$.

Resource: http://www.regular-expressions.info/completelines.html