简体   繁体   English

检查字符串是否等于多个给定字符串之一

[英]Check if string is equal to one of many given strings

I want to check whether string is one of the given strings. 我想检查字符串是否为给定的字符串之一。 It would look something like this. 看起来像这样。

$string 'test';


// if given string is 'test' function will return false.
function checkIfIsSubstring($string)
{
if (strcmp($string, 'test2') )
   return true;
if (strcmp($string, 'test3') )
   return true;
if (strcmp($string, 'test4') )
   return true;
return false;
} 

is there php function what would do same thing, without me creating a new function? 是否有php函数在没有我创建新函数的情况下会做同样的事情?

You can put the values into an array and then use in_array() : 您可以将值放入数组,然后使用in_array()

$array = array('test1', 'test2', 'test3');
$string = 'test1';    

if(in_array($string, $array)) {
    // do something
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM