1<?php
2$file = 'somefile.txt';
3$searchfor = 'name';
4
5// the following line prevents the browser from parsing this as HTML.
6header('Content-Type: text/plain');
7
8// get the file contents, assuming the file to be readable (and exist)
9$contents = file_get_contents($file);
10// escape special characters in the query
11$pattern = preg_quote($searchfor, '/');
12// finalise the regular expression, matching the whole line
13$pattern = "/^.*$pattern.*\$/m";
14// search, and store all matching occurences in $matches
15if(preg_match_all($pattern, $contents, $matches)){
16 echo "Found matches:\n";
17 echo implode("\n", $matches[0]);
18}
19else{
20 echo "No matches found";
21}