0

I have a list of words in an array. What is the fastest way to check if any of these words exist in an string?

Currently, I am checking the existence of array elements one by one through a foreach loop by stripos. I am curious if there is a faster method, like what we do for str_replace using an array.

2
  • do you only want to know if there is a match or do you want to know what the match is (or how often it occurs)? Commented May 21, 2012 at 22:36
  • @MonkeyMonkey I just want to check if they exist; no matter where and how many times. Commented May 21, 2012 at 22:37

3 Answers 3

2

Regarding to your additional comment you could explode your string into single words using explode() or preg_split() and then check this array against the needles-array using array_intersect(). So all the work is done only once.

<?php
$haystack = "Hello Houston, we have a problem";
$haystacks = preg_split("/\b/", $haystack);
$needles = array("Chicago", "New York", "Houston");
$intersect = array_intersect($haystacks, $needles);
$count = count($intersect);

var_dump($count, $intersect);

I could imagine that array_intersect() is pretty fast. But it depends what you really want (matching words, matching fragments, ..)

Sign up to request clarification or add additional context in comments.

Comments

1

my personal function:

function wordsFound($haystack,$needles) {
    return preg_match('/\b('.implode('|',$needles).')\b/i',$haystack);      
}

//> Usage:
if (wordsFound('string string string',array('words')))

Notice if you work with UTF-8 exotic strings you need to change \b with teh corrispondent of utf-8 preg word boundary

Notice2: be sure to enter only a-z0-9 chars in $needles (thanks to MonkeyMonkey) otherwise you need to preg_quote it before

Notice3: this function is case insensitve thanks to i modifier

4 Comments

since my concern is about performance; is preg_match faster than foreach loop?
@Ali: yes for this case. it may depends on the $needles size tho
you should use preg_quote() before that, otherwise there's the possibility of a regex-injection
@MonkeyMonkey: added a notice
0

In general regular expressions are slower compared to basic string functions like str_ipos(). But I think it really depends on the situation. If you really need the maximum performance, I suggest making some tests with real-world data.

2 Comments

stripos is very fast. It is not matter of stripos; the problem is that we need to perform stripos MANY times through the foreach loop.
@Ali: Thanks, I know. As I said you should really compare both approaches using real-world data.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.