Jump to content

Search PDF Body?


dfowler

Recommended Posts

load the file into a string, and do something like this:

<?php
// get contents of a file into a string
$filename = "/usr/local/something.pdf";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

$searchStr = 'hello';
if(preg_match("~$searchStr~",$contents,$matches)){
     echo 'Found: '.$searchStr.' in '.$filename;
}else{
     echo 'Could not find: '.$searchStr.' in '.$filename;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/105471-search-pdf-body/#findComment-540913
Share on other sites

load the file into a string, and do something like this:

<?php
// get contents of a file into a string
$filename = "/usr/local/something.pdf";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

$searchStr = 'hello';
if(preg_match("~$searchStr~",$contents,$matches)){
     echo 'Found: '.$searchStr.' in '.$filename;
}else{
     echo 'Could not find: '.$searchStr.' in '.$filename;
}
?>

Ok, so I could probably pull something similar to this then:

<?php
// get contents of a file into a string
$query = "select name from pdfs'";
$pdfs = array();
$result = mysql_query($query);
while (($row = mysql_fetch_assoc($result)) !== false) {
  $pdfs[] = $row;
}

$contents = array();
$filename = array();
foreach($pdfs as $p) {
  $filename[] = $p['name'];
  $handle = fopen($filename, "r");
  $contents[] = fread($handle, filesize($filename));
  fclose($handle);
}

$searchStr = $_POST['keyword'];
foreach($contents as $c){
if(preg_match("~$searchStr~",$contents,$matches)){
     echo $filename;
}else{
     echo 'Could not find any PDFs that match your search.';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/105471-search-pdf-body/#findComment-540930
Share on other sites

Look fair, the only line I am VERY questionable about is:

 

$contents[] = fread($handle, filesize($filename));

 

I would say something like this may be better:

 

$contents[] = fread($handle, filesize($p['name']));

 

Cool, I know I am saving the PDF location in the database on the backend.  Would it be better to do this:

$filename = $p['location']."/".p['name'];

then the lines:

$handle = fopen($filename, "r");
$contents[] = fread($handle, filesize($filename));

would work better?

 

Link to comment
https://forums.phpfreaks.com/topic/105471-search-pdf-body/#findComment-540948
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.