Find specific text in multiple TXT files in PHP
Solution 1
You can get what you need without the use of grep. Grep is a handy tool for when you are on the commandline but you can do what you need with just a bit of PHP code.
This little snippet for example, gives you results similar to grep:
$path_to_check = '';
$needle = 'match';
foreach(glob($path_to_check . '*.txt') as $filename)
{
foreach(file($filename) as $fli=>$fl)
{
if(strpos($fl, $needle)!==false)
{
echo $filename . ' on line ' . ($fli+1) . ': ' . $fl;
}
}
}
Solution 2
If it is a Unix host you're running on, you can make a system call to grep
in the directory:
$search_pattern = "text to find";
$output = array();
$result = exec("/path/to/grep -l " . escapeshellarg($search_pattern) . " /path/to/directory/*", $output);
print_r($output);
// Prints a list of filenames containing the pattern
Solution 3
If you're on a linux box, you can grep instead of using PHP. For php specifically, you can iterate over the files in a directory, open each as a string, find the string, and save the file if the string exists.
Solution 4
Just specify a file name, get the contents of the file, and do regex matching against the file contents. See this and this for further details regarding my code sample below:
$fileName = '/path/to/file.txt';
$fileContents = file_get_contents($fileName);
$searchStr = 'I want to find this exact string in the file contents';
if ($fileContents) { // file was retrieved successfully
// do the regex matching
$matchCount = preg_match_all($searchStr, $fileContents, $matches);
if ($matchCount) { // there were matches
// $match[0] will contain the entire string that was matched
// $matches[1..n] will contain the match substrings
}
} else { // file retrieval had problems
}
Note: This will work irrespective of whether or not you're on a linux box.
Airikr
Updated on July 22, 2022Comments
-
Airikr almost 2 years
I want to find a specific text string in one or more text files in a directory, but I don't know how. I have Googled quite a long time now and I haven't found anything. Therefor I'm asking you guys how I can fix this?
Thanks in advance.
-
Airikr over 12 yearsI'm using Windows :) But my web host uses Linux. Is it possible to make this to work in Windows too, so I can test it before I upload it to my web host?
-
Michael Berkowski over 12 yearsYou can install grep for Windows gnuwin32.sourceforge.net/packages/grep.htm (and its dependencies listed on same page)
-
Airikr over 12 yearsThanks! That worked like a charmed, but how can I get a specific "code" in a line? One line looks like this:
TME: ...|UID: ...|FNE: ...|MSG: ...|IPA: ...
. The "code" is for exampleUID
. -
ghbarratt over 12 yearsIf I understand you correctly,
if(strpos($line, '|UID:')!==false)
might do what you need. -
Airikr over 12 yearsHow should the echo be within this if?
-
ghbarratt over 12 years
if(strpos($line, '|UID:')!==false) echo 'FOUND UID!';
-
Airikr over 12 yearsThanks! But I want to extract the "result" for the given "code", for example "1320631134.3286" for "TME" in one echo (
echo $fl[0];
or something). -
ghbarratt over 12 yearsIf you only need TME then you could do this:
foreach(explode('|',$fl) as $flp) if(strstr($flp,':',true)=='TME') echo trim(substr($flp,4));
But if you need more than just one of the values you could do this:foreach(explode('|',$fl) as $flp) $line_data[strstr($flp,':',true)] = trim(substr($flp,4)); print_r($line_data);
-
Admin about 10 years@ghbarratt can this work for doc file too ?? Please look into this question stackoverflow.com/questions/23079099/…
-
ghbarratt about 10 years@Rick Smarty - Sorry Rick, the simple answer is no. You will need to use something different to parse doc files, or you will need to convert or save those files as some plain text format. The code I provided assumes the files have only plain text.
-
mwfearnley about 2 yearsNowadays there's a good chance that functions like
exec()
will be disabled for security reasons.