9

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
  • 6,258
  • 15
  • 59
  • 110

4 Answers4

11

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
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • I'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? – Airikr Nov 07 '11 at 03:09
  • 1
    You can install grep for Windows http://gnuwin32.sourceforge.net/packages/grep.htm (and its dependencies listed on same page) – Michael Berkowski Nov 07 '11 at 03:12
  • 1
    Nowadays there's a good chance that functions like `exec()` will be disabled for security reasons. – mwfearnley Feb 25 '22 at 14:03
8

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;
    }
  }
}
Antony
  • 450
  • 1
  • 4
  • 15
ghbarratt
  • 11,496
  • 4
  • 41
  • 41
  • Thanks! 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 example `UID`. – Airikr Nov 07 '11 at 03:27
  • If I understand you correctly, `if(strpos($line, '|UID:')!==false)` might do what you need. – ghbarratt Nov 07 '11 at 03:32
  • How should the echo be within this if? – Airikr Nov 07 '11 at 03:37
  • `if(strpos($line, '|UID:')!==false) echo 'FOUND UID!';` – ghbarratt Nov 07 '11 at 03:38
  • Thanks! 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). – Airikr Nov 07 '11 at 03:41
  • 1
    If 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);` – ghbarratt Nov 07 '11 at 03:51
  • @ghbarratt can this work for doc file too ?? Please look into this question http://stackoverflow.com/questions/23079099/find-sepecific-text-in-multiple-word-document-files –  Apr 15 '14 at 09:23
  • @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. – ghbarratt Apr 15 '14 at 20:51
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.

1

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.

Ryan
  • 1,557
  • 9
  • 11