且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查一个字符串是否包含多个特定单词

更新时间:2022-11-17 10:56:04

为此,您将需要正则表达式 preg_match 功能.

For this, you will need Regular Expressions and the preg_match function.

类似的东西:

if(preg_match('(bad|naughty)', $data) === 1) { } 

您的尝试失败的原因

正则表达式由PHP regex引擎解析.语法的问题是您使用了||运算符.这不是正则表达式运算符,因此将其计为字符串的一部分.

Regular Expressions are parsed by the PHP regex engine. The problem with your syntax is that you used the || operator. This is not a regex operator, so it is counted as part of the string.

正如上面正确指出的,如果它被视为您要匹配的字符串的一部分:'bad || naughty'作为字符串,而不是表达式!

As correctly stated above, if it's counted as part of the string you're looking to match: 'bad || naughty' as a string, rather than an expression!