在 PHP 中,将字符串与关键字列表匹配的最有效方法是什么? [英] In PHP, what is the most efficient way to match a string against a list of keywords?

查看:54
本文介绍了在 PHP 中,将字符串与关键字列表匹配的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关键字列表,需要检查这些关键字是否出现在字符串中.例如:

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

我可以将我的关键字放在一个数组中,遍历它并在每次迭代时执行 preg_match() 或 substr(),但这似乎有点 CPU 成本.我对正则表达式进行了一些处理,但没有取得多大成功.

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.

执行此操作的最有效方法是什么(就精简代码和低 CPU 负载而言)?

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.

推荐答案

包含所有备选方案的正则表达式将确保对字符串扫描一次,而不是对 N 个关键字进行 N 次扫描.PCRE 库优化得很好.

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

终于有

preg_grep($regex, $array_of_strings);

将匹配一个字符串数组并返回匹配的字符串.

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

这篇关于在 PHP 中,将字符串与关键字列表匹配的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆