测试某些单词的字符串的有效方法 [英] Efficient way to test string for certain words

查看:78
本文介绍了测试某些单词的字符串的有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆被禁止的单词,想检查字符串A是否包含这些单词中的任何一个.

I have a bunch of banned words and want to check if string A contains any of these words.

例如:

$banned_words = "dog cat horse bird mouse monkey blah blah2 blah3 "; //etc
$string_A = "The quick brown fox jumped over the lazy dog";

如何有效地检查字符串中的任何单词是否与禁止单词列表中的任何单词匹配?

How can I efficiently check to see if any of the words in the string match up with any of the words in the banned words list?

推荐答案

if (preg_match('~\b(' . str_replace(' ', '|', $banned_words) . ')\b~', $string_A)) {
    // there is banned word in a string
}

这篇关于测试某些单词的字符串的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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