dongwu1992 2012-11-15 01:42
浏览 33

PHP shell_exec不一致

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.

I'm open for other suggestions (see below), but at present my approach is to use mdfind on OS X, like this:

$finds = array();
foreach ($search as $term) {
    $result = "";
    $cleanResult = array();
    $shellQuery = "mdfind -onlyin \"$wd\" \"kind:pdf $term\"";
    echo "

$shellQuery
";
    $result = shell_exec($shellQuery);
    echo $result;
    $cleanResult = split("
", $result);
    array_pop($cleanResult);
    $finds[$term] = $cleanResult;

    unset($result);
    unset($cleanResult);
}
print_r($finds);

However, although this builds $shellQuery just fine, for some reason $result doesn't always get populated even when the command works (i.e. if I copy and paste the value of $shellQuery into a terminal window, it works as expected).

Let's say $search 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?!

Incidentally, my preferred approach would be to do something like:

find . -name "*.pdf*" -exec pdftotext {} - \; | grep -i -l "foo"

but I can't get this to work in Terminal. I've installed http://www.bluem.net/en/mac/packages/ (I struggle to compile things, so packages like this = thumbs up!), but every time I try and pipe this to grep (e.g. pdftotext myfile.pdf - | grep -i -l "foo") grep just returns (standard output) and no more.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?
    • ¥100 求三轴之间相互配合画圆以及直线的算法
    • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
    • ¥15 名为“Product”的列已属于此 DataTable
    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题