In PHP, if I find a word in a file, can I make the line that the word came from into a $string

Solution:1

Use a line-delimited regular expression to find the word, then your match will contain the whole line.

Something like:

preg_match('^.*WORD.*$, $filecontents, $matches);

Then $matches will have the full lines of the places it found WORD

Solution:2

$path = "/path/to/wordlist.txt";
$word = "Word";

$handle = fopen($path,'r');
$currentline = 1;  //in case you want to know which line you got it from
while(!feof($handle))
{
    $line = fgets($handle);
    if(strpos($line,$word))
    {
        $lines[$currentline] = $line;
    }
    $currentline++;
}
fclose($handle);

If you want to only find a single line where the word occurs, then instead of saving it to an array, save it somewhere and just break after the match is made.

This should work quickly on files of any size (using file() on large files probably isn’t good)