In PHP, what is the most effective way to match a string to a list of keywords?

advertisements

I have a list of keywords, and need to check whether any of these occurs in a string. E.g.:

/* Keywords */
Rock
Paper
Scissors

/* Strings */
"This town rocks!"    /* Match */
"Paper is patient"    /* Match */
"Hello, world!"       /* No match */

I could put my keywords in an array, loop through it and do a preg_match() or substr() on each iteration, but that seems a bit cpu-expensive. I've mucked aroud with regexps a bit, but without much success.

What is the most efficient way (in terms of lean code and low CPU loads) to do this?

Note that the comparison must be case-insensitive.


A regex with all alternatives will ensure string is scanned once, rather than N times for N keywords. PCRE library is very well optimized.

preg_match('/rock|paper|scissors/i', $string);

It gets faster if your keywords have common prefixes and you take advantage of that (essentially by building a trie and inlining it):

preg_match('/rock|paper|sci(?:ssors|ence)/i', $string);

And finally there's

preg_grep($regex, $array_of_strings);

that will match against an array of strings and return ones that match.