在多个Word文档文件中查找单独的文本 [英] Find sepecific text in multiple word document files
问题描述
我想构建一个搜索模块,其中用户输入文本,并且该文本应搜索特定目录中的所有文件.我已经使用了这段代码:
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;
}
}
}
但是它仅适用于.txt文件,应该在.doc文件中进行搜索.我也将 glob($ path_to_check.'*.txt')作为$ filename)
更改为 glob($ path_to_check.'*.doc')作为$ filename)
它不显示结果.请帮助我.
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.
我还尝试了此
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]);
它显示Array(),我不知道在完整或相对目录"之间放置什么,我是指路径.
It shows Array(),I dont know what to put between "full-or-relative-directory" I mean the path.
我的代码:-
php > exec("egrep -rl 'rakesh' E:/xampp/htdocs/talent_orbit/test/", $output);
php > print_r($output);
如果不可能,我可以将doc文件转换为txt文件,然后在该txt文件中进行搜索吗?
谢谢.
推荐答案
这是不可能的.文档文件不是"纯文本"文件.尝试在编辑器中打开它,您会看到.搜索 *.txt
和 *.xml
文件将是可行的,因为它们基本上都是纯文本文件.一个doc文件中包含二进制数据.
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.
一种解决方案将是PHP的文档解析器(例如这一个),但是它将需要一个遍历文件的脚本,使用解析器打开每个文件并搜索字符串.
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.
这篇关于在多个Word文档文件中查找单独的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!