Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP shell_exec inconsistent
    text
    copied!<p>I'm trying to search through PDF files for multiple key words. I've got ~60 PDFs and ~8 key words and don't fancy ~480 manual searches.</p> <p>I'm open for other suggestions (see below), but at present my approach is to use <code>mdfind</code> on OS X, like this:</p> <pre><code>$finds = array(); foreach ($search as $term) { $result = ""; $cleanResult = array(); $shellQuery = "mdfind -onlyin \"$wd\" \"kind:pdf $term\""; echo "\n\n$shellQuery\n"; $result = shell_exec($shellQuery); echo $result; $cleanResult = split("\n", $result); array_pop($cleanResult); $finds[$term] = $cleanResult; unset($result); unset($cleanResult); } print_r($finds); </code></pre> <p>However, although this builds <code>$shellQuery</code> just fine, for some reason <code>$result</code> doesn't always get populated even when the command works (i.e. if I copy and paste the value of <code>$shellQuery</code> into a terminal window, it works as expected).</p> <p>Let's say <code>$search</code> contains 'foo', 'bar' and 'joe', it might find 'foo' and 'joe' fine, but return nothing for 'bar'. If I remove 'foo' and 'joe' from the array and just search for 'bar', it'll find 'bar' fine. Does it need a rest between calls or something?!</p> <p>Incidentally, my preferred approach would be to do something like:</p> <p><code>find . -name "*.pdf*" -exec pdftotext {} - \; | grep -i -l "foo"</code></p> <p>but I can't get this to work in Terminal. I've installed <a href="http://www.bluem.net/en/mac/packages/" rel="nofollow">http://www.bluem.net/en/mac/packages/</a> (I struggle to compile things, so packages like this = thumbs up!), but every time I try and pipe this to grep (e.g. <code>pdftotext myfile.pdf - | grep -i -l "foo"</code>) grep just returns <code>(standard output)</code> and no more.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload