Find text in multiple Word document files

I want to create a search module in which the user enters text, and this text should search for all files in a specific directory. I 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 only works for a .txt file, it should search in a .doc file. I 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 with this.

EDIT:

I also tried a 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 do not know what to put between the "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 this is not possible, is it possible to convert the doc file to a txt file and then search in that txt file?

Thanks in advance.

+1
source share
1 answer

It's impossible. The doc file is not a plain text file. Try opening it in your editor and you will see. Searching through *.txt and *.xml files will work, because these are basically all plaintext files. The doc file has binary data in it.

The solution will be a doc parser for PHP (for example, this one ), but for this you need a script that goes through the files, open each file with a parser and look for a line.

0
source

All Articles