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.
The general notion…
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';}
…I might try to find the strings ‘apple’,’orange’,’banana’ in a regular sentence/string.
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';}
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: