1

I want to build a search module in which user enter a text and that text should search all files in particular directory. I have used this code :

$path_to_check = 'E:/xampp/htdocs/talent_orbit/test/';
$needle = 'test';

foreach(glob($path_to_check.'*.txt') as $filename)
{
   //print_r(file($filename));
  foreach(file($filename) as $fli=>$fl)
  {
      echo $f1;
    if(strpos($fl, $needle)!==false)
    {
      echo $filename.' on line '.($fli+1).': '.$fl;
    }
  }
}

But it works only for .txt file, it should search in .doc file. I have also change glob($path_to_check.'*.txt') as $filename) to glob($path_to_check.'*.doc') as $filename) but it does not show the result. Please help me in this.

EDIT :

I also tried ont the solution from this

php > exec("egrep -rl 'string of what I want to find' full-or-relative-directory", $output);
php > print_r($output);
Array
(
  [0] => full-or-relative-directory/foo/bar.xml
)
php > $contents = file_get_contents($output[0]);

It shows Array(),I dont know what to put between "full-or-relative-directory" I mean the path.

My code :-

php > exec("egrep -rl 'rakesh' E:/xampp/htdocs/talent_orbit/test/", $output);
php > print_r($output);

If it is not possible then can I convert doc file into txt file and then search in that txt file ?

Thanks in advance.

Community
  • 1
  • 1

1 Answers1

0

This is not possible. A doc file isn't a 'plain text' file. Try opening it in your editor and you will see. Searching through *.txt and *.xml files will work, because those are basically all plaintext files. A doc file has binary data in it.

A solution would be a doc-parser for PHP (for example this one), but it will require a script that loops through the files, open each and every file with the parser and search for the string.

trizz
  • 1,447
  • 12
  • 27
  • thanks for your reply so can I convert doc file to txt file and then search in that txt file ? –  Apr 15 '14 at 09:54
  • I guess that is possible. You can save your document in Word as `txt` I suppose. But you will lose all of the format and styling that way. – trizz Apr 15 '14 at 10:10
  • that will be ok for me can you tell me how to convert doc file to txt file ? –  Apr 15 '14 at 10:15
  • See [this question](http://stackoverflow.com/questions/188452/reading-writing-a-ms-word-file-in-php) for more information on reading DOC-files with PHP. This way, if you get the contents, you can save them to a txt file and search it the way you want. – trizz Apr 15 '14 at 10:18