Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 598
Frank van Wensveen Avatar asked Sep 18 '25 09:09

Frank van Wensveen


2 Answers

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.

like image 73
Kornel Avatar answered Sep 20 '25 23:09

Kornel


Just to see if any keyword is found you could do this with keywords as an array:

if(str_ireplace($keyword_array, '', $string) != $string) {
    //match
} else {
    //no match
}
like image 45
AbraCadaver Avatar answered Sep 20 '25 21:09

AbraCadaver