Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find specific text in multiple TXT files in PHP

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.

like image 356
Airikr Avatar asked Nov 07 '11 02:11

Airikr


1 Answers

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
like image 113
Michael Berkowski Avatar answered Oct 16 '22 08:10

Michael Berkowski