html - What would be the best way to search a txt file display all result on page in php -


i have been trying create page can search .txt file using html , php.

here code:

<?php $file = 'myfile.txt'; if (preg_match_all($pattern, $contents, $matches)) {    echo "found matches:\n";    echo implode("\n", $matches[0]); } else {   echo "no matches found"; } 

please try code. code missing $contents

<?php $file = 'somefile.txt'; $searchfor = 'search text come here';  // file contents $contents = file_get_contents($file);  // escape special characters in query $pattern = preg_quote($searchfor, '/');  // regular expression $pattern = "/^.*$pattern.*\$/m";  // search if(preg_match_all($pattern, $contents, $matches)){    echo "found matches:\n";    echo implode("\n", $matches[0]); } else{    echo "no matches found"; } 

thanks amit


Comments