使用 PHP 在文件中搜索文本

发布于 2024-09-26 01:05:24 字数 168 浏览 0 评论 0原文

如何使用 PHP 搜索 PDF、doc、docs 或 txt 等文件中的文本? 我想在MySQL中做类似全文搜索的功能, 但这一次,我直接通过文件搜索,而不是数据库。

搜索将在位于文件夹中的许多文件中进行搜索。 对于这个问题有什么建议、技巧或解决方案吗?

我还注意到,谷歌也会搜索这些文件。

How to search text in some files like PDF, doc, docs or txt using PHP?
I want to do similar function as Full Text Search in MySQL,
but this time, I'm directly search through files, not database.

The search will do searching in many files that located in a folder.
Any suggestion, tips or solutions for this problem?

I also noticed that, google also do searching through the files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

莫言歌 2024-10-03 01:05:24

要搜索 PDF,您需要一个像 pdftotext 这样的程序,它将内容从 pdf 转换为文本。对于 Word 文档,可以使用类似的东西(因为 Word 文件中的所有样式和加密)。

一个搜索 PDF 的示例(从我的一个脚本复制(它是一个片段,不是整个代码,但它应该让您有一些理解),我在其中提取关键字并将匹配项存储在 PDF 结果数组中。):

foreach($keywords as $keyword)
{
    $keyword = strtolower($keyword);
    $file = ABSOLUTE_PATH_SITE."_uploaded/files/Transcripties/".$pdfFiles[$i];

    $content    = addslashes(shell_exec('/usr/bin/pdftotext \''.$file.'\' -'));
    $result     = substr_count(strtolower($content), $keyword);

    if($result > 0)
    {
        if(!in_array($pdfFiles[$i], $matchesOnPDF))
        {
            array_push($matchesOnPDF, array(                                                    
                    "matches"   => $result,
                    "type"      => "PDF",
                    "pdfFile"   => $pdfFiles[$i]));
        }
    }
}

For searching PDF's you'll need a program like pdftotext, which converts content from a pdf to text. For Word documents a simular thingy could be available (because of all the styling and encryption in Word files).

An example to search through PDF's (copied from one of my scripts (it's a snippet, not the entire code, but it should give you some understanding) where I extract keywords and store matches in a PDF-results-array.):

foreach($keywords as $keyword)
{
    $keyword = strtolower($keyword);
    $file = ABSOLUTE_PATH_SITE."_uploaded/files/Transcripties/".$pdfFiles[$i];

    $content    = addslashes(shell_exec('/usr/bin/pdftotext \''.$file.'\' -'));
    $result     = substr_count(strtolower($content), $keyword);

    if($result > 0)
    {
        if(!in_array($pdfFiles[$i], $matchesOnPDF))
        {
            array_push($matchesOnPDF, array(                                                    
                    "matches"   => $result,
                    "type"      => "PDF",
                    "pdfFile"   => $pdfFiles[$i]));
        }
    }
}
千紇 2024-10-03 01:05:24

根据文件类型,您应该将文件转换为文本,然后使用 file_get_contents()str_pos() 进行搜索。要将文件转换为文本,除了其他工具外,您还可以使用以下工具:

  • catdoc 用于 Word 文件
  • xlhtml 用于 Excel 文件
  • ppthtml 用于 Powerpoint 文件
  • unrtf 用于 RTF 文件
  • pdftotext 用于 pdf 文件

Depending on the file type, you should convert the file to text and then search through it using i.e. file_get_contents() and str_pos(). To convert files to text, you have - beside others - the following tools available:

  • catdoc for word files
  • xlhtml for excel files
  • ppthtml for powerpoint files
  • unrtf for RTF files
  • pdftotext for pdf files
如歌彻婉言 2024-10-03 01:05:24

如果您在 Linux 服务器下,您可以使用

grep -R "text to be searched for" ./   // location is everything under the actual directory

exec 从 php 调用,从而导致

cmd = 'grep -R "text to be searched for" ./';
$result = exec(grep);
print_r(result);

If you are under a linux server you may use

grep -R "text to be searched for" ./   // location is everything under the actual directory

called from php using exec resulting in

cmd = 'grep -R "text to be searched for" ./';
$result = exec(grep);
print_r(result);
临风闻羌笛 2024-10-03 01:05:24

2021 我遇到了这个并发现了一些东西,所以我想我会链接到它...

注意:docx、pdf 和其他文件不是常规文本文件,需要更多脚本和/或不同的库来读取和/或编辑每种不同类型,除非您可以找到一个全合一的图书馆。这意味着您必须编写要搜索的每种不同文件类型的脚本,尽管包括普通文本文件。如果您不想完全编写脚本,那么您必须安装您想要读取的每种文件类型所需的每个库。但您仍然需要编写每个脚本来将它们作为库函数进行处理。

我在堆栈上此处找到了基本答案。

2021 I came across this and found something so I figure I will link to it...

Note: docx, pdfs and others are not regular text files and require more scripting and/or different libraries to read and/or edit each different type unless you can find an all in one library. This means you would have to script out each different file type you want to search though including a normal text file. If you don't want to script it completely then you have to install each of the libraries you will need for each of the file types you want to read as well. But you still need to script each to handle them as the library functions.

I found the basic answer here on the stack.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文