PHPコードを使用してpdfファイル内の文字列を検索しようとしています。TEXT ファイル内の文字列を検索する php コードを作成しました。このコードは、目的の検索文字列を取得し、この文字列を TEXT ファイルの内容と一致させ、文字列がテキスト ファイルの内容と一致する完全な行を返します。
私がPDFファイルでやりたいこのような種類の作業ですが、何の手がかりも得られず、クラスclass.pdf2text.phpを検索しましたが、このクラスはpdfファイルのコンテンツ全体をテキスト形式で返します。
このタスクを完了するには、ガイダンスと提案が必要です。
ここに私のコードがあります
include('class.pdf2text.php');
$a = new PDF2Text();
$a->setFilename('file.pdf');
$a->decodePDF();
$data = $a->output();// stored the whole text into a variable
$myfile = "file.txt";
$fh = fopen($myfile, 'w');
fwrite($fh, $data);// write all the text into a text file
$file = 'file.txt';
$searchfor = 'string to search';
header('Content-Type: text/plain');
$result = getLineWithString($file, $searchfor);// search test string in text file
function getLineWithString($file, $searchfor) {
$lines = file($file);
foreach ($lines as $line) {
if (strpos($line, $searchfor) !== false) {
return $line;enter code here
}
}
return -1;
}
echo $result;