dfowler Posted May 13, 2008 Share Posted May 13, 2008 Does anybody know if it is possible to create a search that will look within a PDF body? Or is there any 3rd party software that can perform this? Link to comment https://forums.phpfreaks.com/topic/105471-search-pdf-body/ Share on other sites More sharing options...
BlueSkyIS Posted May 13, 2008 Share Posted May 13, 2008 most PDF files are essentially text files. the text within is typically stored unencoded. so you can search most PDFs just like you would a plain text file. Link to comment https://forums.phpfreaks.com/topic/105471-search-pdf-body/#findComment-540267 Share on other sites More sharing options...
dfowler Posted May 14, 2008 Author Share Posted May 14, 2008 So how would I go about being able to do this? Most searches I've created have pulled information from a database. Not a specific file. Link to comment https://forums.phpfreaks.com/topic/105471-search-pdf-body/#findComment-540900 Share on other sites More sharing options...
The Little Guy Posted May 14, 2008 Share Posted May 14, 2008 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 More sharing options...
dfowler Posted May 14, 2008 Author Share Posted May 14, 2008 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 More sharing options...
The Little Guy Posted May 14, 2008 Share Posted May 14, 2008 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'])); Link to comment https://forums.phpfreaks.com/topic/105471-search-pdf-body/#findComment-540938 Share on other sites More sharing options...
dfowler Posted May 14, 2008 Author Share Posted May 14, 2008 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 More sharing options...
The Little Guy Posted May 14, 2008 Share Posted May 14, 2008 Sure. Link to comment https://forums.phpfreaks.com/topic/105471-search-pdf-body/#findComment-540965 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.