Searching Inside A File Using Php

[Solved] Searching Inside A File Using Php | Php - Code Explorer | yomemimo.com
Question : searching inside a file using php

Answered by : friendly-ferret-3liqcxrky19d

<?php
$file = 'somefile.txt';
$searchfor = 'name';
// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');
// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$pattern = preg_quote($searchfor, '/');
// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*\$/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){ echo "Found matches:\n"; echo implode("\n", $matches[0]);
}
else{ echo "No matches found";
}

Source : https://stackoverflow.com/questions/3686177/php-to-search-within-txt-file-and-echo-the-whole-line | Last Update : Fri, 05 Jun 20

Answers related to searching inside a file using php

Code Explorer Popular Question For Php