如果条件中没有多个运算符,请使用stristr匹配单个数组中的任何值 [英] Use stristr to match any value in array in a SINGLE if condition without multiple operators
问题描述
我在编码PHP时非常有能力,尽管我遇到一种情况,我想知道是否可以使用stristr
匹配 SINGLE 中的数组中的任何值(如果有条件) .我不需要/想要一个类,一个专用函数或任何超级复杂的东西,也不需要做任何复杂的事情,例如尝试匹配特殊字符,我只是觉得我可能已经错过了php.net上有用的东西.
I'm pretty capable when coding PHP though I just came across a situation where I wondered if it would be possible to use stristr
to match any value in an array inside a SINGLE if condition. I don't need/want a class, a dedicated function or anything super-complex nor am I doing anything complex such as trying to match special characters, I just think I may have missed something useful over at php.net.
一般概念...
if (stristr($search_this,$array_with_values)) {echo 'a match was found';}
...或等于什么(即使这不起作用)...
...or what would amount to (even though this does not work)...
if (stristr($search_this,array('string1','string2'))) {echo 'a match was found';}
...我可能会尝试在常规句子/字符串中找到字符串'apple','orange','banana'.
...I might try to find the strings 'apple','orange','banana' in a regular sentence/string.
我可以在if条件之外定义数组.
I can define the array outside of the if condition.
我的主要目标是在条件不多的情况下使用多个运算符,例如&&或||.
My main goal is to simply not have numerous if conditions or use multiple operators such as && or ||.
所以我不想有多个||.运营商可能会这样...
So I do not want to have multiple || operators like this if possible...
if (
stristr($search_this_string,'string1') ||
stristr($search_this_string,'string2') ||
stristr($search_this_string,'string3')
) {echo 'a match was found';}
我不介意在if条件之前/之外放置数组,尽管我希望能够将所有其他条件都放在单个if条件之内.
I don't mind having the array before/outside of the if condition though I want to be able to fit everything else inside of a single if condition.
推荐答案
我怀疑这是高性能的,但是如果您正在寻找一种外观简单的单线纸:
I doubt this is performant, but if you are looking for a reasonably simple-looking one-liner:
if( str_ireplace(array('string1', 'string2'), '', $str) != $str )
{
echo 'a match was found';
}
这篇关于如果条件中没有多个运算符,请使用stristr匹配单个数组中的任何值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!