PHP strpos匹配多个干草堆中的所有针

时间:2015-02-27 03:51:16

标签: php arrays match strpos

我想检查$ words中的所有单词是否存在于一个或多个$句子中,单词顺序并不重要。

单词只包含[a-z0-9]。

句子只包含[a-z0-9 - ]。

到目前为止,我的代码几乎按预期工作:

$words = array("3d", "4");
$sentences = array("x-3d-abstract--part--282345", "3d-speed--boat-430419", "beautiful-flower-462451", "3d-d--384967");

foreach ($words as $word) {
    $sentences_found = array_values(array_filter($sentences, function($find_words) use ($word) {return strpos($find_words, $word);}));
}
print_r($sentences_found);

如果您在此处运行此代码http://3v4l.org/tD5t5,您将获得4个结果,但实际上应该是3个结果

Array
(
    [0] => x-3d-abstract--part--282345
    [1] => 3d-speed--boat-430419
    [2] => beautiful-flower-462451   // this one is wrong, no "3d" in here, only "4"
    [3] => 3d-d--384967
)

我该怎么做?

还有比strpos更好的方法吗?

正则表达式?

正则表达式对于这份工作来说可能很慢,因为有时会有1000多个$句子(不要问为什么)。

1 个答案:

答案 0 :(得分:1)

你可以使用每个单词的找到句子的交集:

$found = array();

foreach ($words as $word) {
    $found[$word] = array_filter($sentences, function($sentence) use ($word) {
        return strpos($sentence, $word) !== false;
    });
}

print_r(call_user_func_array('array_intersect', $found));

或者,从$sentences

接近
$found = array_filter($sentences, function($sentence) use ($words) {
    foreach ($words as $word) {
        if (strpos($sentence, $word) === false) {
            return false;
        }
    }
    // all words found in sentence 
    return true;
});

print_r($found);

值得一提的是,您的搜索条件错误;您应该明确地与strpos($sentence, $word)进行比较,而不是false,否则您将错过句子开头的匹配。

相关问题