【问题标题】:Find sepecific text in multiple word document files在多个 Word 文档文件中查找特定文本
【发布时间】:2014-04-15 09:04:14
【问题描述】:

我想建立一个搜索模块,用户在其中输入一个文本,该文本应该搜索特定目录中的所有文件。我用过这段代码:

$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') as $filename) 更改为glob($path_to_check.'*.doc') as $filename),但它没有显示结果。请帮帮我。

编辑:

我也尝试了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(),我不知道在“完整或相对目录”之间放置什么,我的意思是路径。

我的代码:-

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

如果不可能,我可以将 doc 文件转换为 txt 文件,然后在该 txt 文件中搜索吗?

提前致谢。

【问题讨论】:

  • 更改为 .doc 会出现什么错误?
  • 什么都没有.. 它显示为空白

标签: php file


【解决方案1】:

这是不可能的。 doc 文件不是“plain text”文件。尝试在您的编辑器中打开它,您会看到。搜索*.txt*.xml 文件会起作用,因为它们基本上都是纯文本文件。一个 doc 文件中包含二进制数据。

一个解决方案是 PHP 的文档解析器(例如 this one),但它需要一个循环文件​​的脚本,使用解析器打开每个文件并搜索字符串。

【讨论】:

  • 感谢您的回复,我可以将doc文件转换为txt文件,然后在该txt文件中搜索吗?
  • 我想这是可能的。我想您可以将您的文档保存在 Word 中为txt。但是这样你会失去所有的格式和样式。
  • 这对我来说没问题,你能告诉我如何将 doc 文件转换为 txt 文件吗?
  • 有关使用 PHP 读取 DOC 文件的更多信息,请参阅 this question。这样,如果你得到了内容,你可以将它们保存到一个 txt 文件中,然后按照你想要的方式进行搜索。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-23
  • 2021-04-27
  • 1970-01-01
  • 2013-05-03
  • 2017-08-06
  • 1970-01-01
相关资源
最近更新 更多