Solution 1:
You can use a line-based regular expression to match the word and capture the entire line that contains it. For example:
preg_match('/^.*WORD.*$/m', $filecontents, $matches);
This way, $matches will contain the full line(s) where WORD was found.
🔹 Notes:
The ^ and $ anchors match the start and end of a line.
The m (multiline) modifier makes ^ and $ apply to each line within the string, not just the beginning and end of the whole string.
If you expect multiple matches, use preg_match_all() instead of preg_match().
preg_match_all('/^.*WORD.*$/m', $filecontents, $matches);
Then $matches[0] will contain all matching lines.