php 检查字符串是否包含多个特定单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15862361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a string contain multiple specific words
提问by Jessica Lingmn
How to check, if a string contain multiple specific words?
如何检查字符串是否包含多个特定单词?
I can check single words using following code:
我可以使用以下代码检查单个单词:
$data = "text text text text text text text bad text text naughty";
if (strpos($data, 'bad') !== false) {
echo 'true';
}
But, I want to add more words to check. Something like this:
但是,我想添加更多单词来检查。像这样的东西:
$data = "text text text text text text text bad text text naughty";
if (strpos($data, 'bad || naughty') !== false) {
echo 'true';
}
?>
(if any of these words is found then it should return true)
(如果找到这些词中的任何一个,那么它应该返回真)
But, above code does not work correctly. Any idea, what I'm doing wrong?
但是,上面的代码不能正常工作。知道吗,我做错了什么?
回答by christopher
For this, you will need Regular Expressionsand the preg_matchfunction.
为此,您将需要正则表达式和preg_match函数。
Something like:
就像是:
if(preg_match('(bad|naughty)', $data) === 1) { }
The reason your attempt didn't work
您的尝试无效的原因
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.
正则表达式由 PHP 正则表达式引擎解析。您的语法问题在于您使用了||
运算符。这不是正则表达式运算符,因此它被视为字符串的一部分。
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!
如上所述,如果它被算作您要匹配的字符串的一部分:'bad || naughty'
作为字符串,而不是表达式!
回答by Jessica Lingmn
You can't do something like this:
你不能做这样的事情:
if (strpos($data, 'bad || naughty') !== false) {
instead, you can use regex:
相反,您可以使用正则表达式:
if(preg_match("/(bad|naughty|other)/i", $data)){
//one of these string found
}
回答by Eineki
strposdoes search the exact string you pass as second parameter. If you want to check for multiple words you have to resort to different tools
strpos确实搜索您作为第二个参数传递的确切字符串。如果要检查多个单词,则必须使用不同的工具
regular expressions
常用表达
if(preg_match("/\b(bad|naughty)\b/", $data)){
echo "Found";
}
(preg_matchreturn 1 if there is a match in the string, 0 otherwise).
(如果字符串中有匹配项,则preg_match返回 1,否则返回 0)。
multiple str_pos calls
多个 str_pos 调用
if (strpos($data, 'bad')!==false or strpos($data, 'naughty')!== false) {
echo "Found";
}
explode
爆炸
if (count(array_intersect(explode(' ', $data),array('bad','naugthy')))) {
echo "Found";
}
The preferred solution, to me, should be the first. It is clear, maybe not so efficient due to the regex use but it does not report false positives and, for example, it will not trigger the echo if the string contains the word badmington
对我来说,首选的解决方案应该是第一个。很明显,由于使用正则表达式,可能效率不高,但它不会报告误报,例如,如果字符串包含单词badmington,它不会触发回声
The regular expression can become a burden to create if it a lot of words (nothing you cannot solve with a line of php though $regex = '/\b('.join('|', $badWords).')\b/';
如果正则表达式很多的话,它可能会成为创建的负担(尽管用一行 php 解决不了任何问题) $regex = '/\b('.join('|', $badWords).')\b/';
The second one is straight forward but can't differentiate badfrom badmington.
第二个是直线前进,但不能区分坏的羽毛球设施。
The third split the string in words if they are separated by a space, a tab char will ruins your results.
第三个将字符串拆分为单词,如果它们由空格分隔,则制表符会破坏您的结果。
回答by magento4u_com
if(preg_match('[bad|naughty]', $data) === true) { }
if(preg_match('[bad|naughty]', $data) === true) { }
The above is not quite correct.
以上并不完全正确。
"preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred."
“如果模式匹配给定主题,则 preg_match() 返回 1,如果不匹配则返回 0,如果发生错误则返回 FALSE。”
So it should be just:
所以它应该只是:
if(preg_match('[bad|naughty]', $data)) { }
回答by Jaakko Kaski
You have to strpos each word. Now you are checking if there is a string that states
您必须对每个单词进行 strpos。现在您正在检查是否有一个字符串表明
'bad || naughty'
which doesn't exist.
这是不存在的。
回答by candle
substr_count()
substr_count()
I want to add one more way doing it with substr_count()
(above all other answers):
我想添加另一种方法substr_count()
(高于所有其他答案):
if (substr_count($data, 'bad') || substr_count($data, 'naughty')){
echo "Found";
}
substr_count()
is counting for how many times the string appears, so when it's 0 then you know that it was not found.
I would say this way is more readable than using str_pos()
(which was mentioned in one of the answers) :
substr_count()
计算字符串出现的次数,所以当它是 0 时,你就知道它没有找到。我会说这种方式比使用str_pos()
(在其中一个答案中提到)更具可读性:
if (strpos($data, 'bad')!==false || strpos($data, 'naughty')!== false) {
echo "Found";
}