【问题标题】:how to read content of pdf files and compare String with user input?如何读取 pdf 文件的内容并将字符串与用户输入进行比较?
【发布时间】:2017-11-25 08:10:19
【问题描述】:

我有一个 PHP 代码,允许用户从指定的目的地选择 PDF 文件。我需要允许用户输入一个字符串作为用户输入,并使系统读取所有 pdfs 文件的内容,如果字符串存在于任何 PDF 文件中,则显示文件名。

到目前为止,我只能选择 PDF 文件。

代码:

<?php

    //path to directory
$directory = $_SERVER['DOCUMENT_ROOT']."/testwebsite\OSWebProject/";
$pdfs= glob($directory. "*.pdf") or DIE("Unable to open $directory");;

 if(isset($_POST["search"]))
{
     $NAME =$_POST['name'];
     echo $NAME ;

    foreach($pdfs as $pdfname ){
        echo $pdfname;

    }
} 
?>
<html>
    <head>

    </head>
    <body>
     <form action="test.php" method="post">
          <p>enter your string <input type ="text"  id = "idName"  name="name" /></p>
          <p><input type ="Submit" name ="search" value= "Search" /></p>
    </form>
    </body>
</html>

【问题讨论】:

  • 听起来像是 pdfgrep 的工作。

标签: php file pdf


【解决方案1】:

您可以尝试使用 pdf-to-text 库将所有​​ PDF 文件转换为文本文件:

https://github.com/spatie/pdf-to-text

然后将文本内容存储在数据库中并进行搜索查询。

$statement = $pdo->prepare("SELECT * FROM pdf_files WHERE text_content LIKE :search");
$statement->execute(['search' => '%' . $search . '%']);

foreach ($statement as $row) {
    // do something with $row
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多