简体   繁体   中英

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. Eg:

/* 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.

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
}

If you don't know your keywords in advance, and you have multiple strings you wish to search through, you can implode your keywords to a regex and grep the strings:

$keywords = array ('Rock', 'Paper', 'sciSSors');
$strings  = array (
    "This town rocks!",
    "Hello, world!",
    "Paper is patient",
);

$rc = preg_grep(
    sprintf('/%s/i', implode('|', array_map('preg_quote', $keywords))),
    $strings
);

/**
array(2) {
  [0]=>
  string(16) "This town rocks!"
  [2]=>
  string(16) "Paper is patient"
}
*/

See it here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM