3

I want to check if ALL words from $words exist in one or more $sentences, word order is not important.

Words will only contain [a-z0-9].

Sentences will only contain [a-z0-9-].

My code so far, it almost work as expected:

$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);

If you run this code here http://3v4l.org/tD5t5 , you'll get 4 results, but in reality it should be 3 results

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
)

How can I do this?

Also is there any better way to do this than strpos?

Regex?

Regex is maybe slow for this job because sometimes there will be 1000's of $sentences (don't ask why).

7
  • 1
    huh? I run your codes, result of print_r() is: Array ( [0] => this-is-simple-simple-sentence-123-aa99-311qwerty ) instead of 3 items
    – Raptor
    Feb 27, 2015 at 3:58
  • are you sure about it? i get all 3 results with print_r()
    – Sunny
    Feb 27, 2015 at 4:10
  • 1
    @Sunny Works also for me! (See: 3v4l.org/b2AoN)
    – Rizier123
    Feb 27, 2015 at 4:14
  • i think i need to re-check my code then, this code i showed you is just a part of bigger code, something is interfering with my results :) at least this code work as expected then :)
    – Sunny
    Feb 27, 2015 at 4:17
  • ok i tested just this code on my server and it works perfectly, so i answered my own question then haha
    – Sunny
    Feb 27, 2015 at 4:20

1 Answer 1

1

You could use an intersection of found sentences per word:

$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));

Or, approach from $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);

One important thing to mention is that your search criteria was wrong; instead of strpos($sentence, $word) you should explicitly compare against false, otherwise you will miss a match at the start of a sentence.

1
  • 1
    amazing work! tyvm it works perfectly, i've tried various combinations and everything is ok, i'll give you the biggest upvote ever! :)
    – Sunny
    Feb 27, 2015 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.