检查字符串是否等于许多给定字符串之一
问题描述:
我想检查字符串是否是给定字符串之一。它看起来像这样。检查字符串是否等于许多给定字符串之一
$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;
}
有没有PHP函数什么会做同样的事情,没有我创建一个新的功能?
答
你可以把这些值到一个数组,然后使用in_array()
:
$array = array('test1', 'test2', 'test3');
$string = 'test1';
if(in_array($string, $array)) {
// do something
}
是,'in_array'。文档页面中的第一个示例正是您要做的。 – Jon 2014-08-30 16:21:18
你检查的字符串是否总是相同? – DanDotNet 2014-08-30 16:56:39