$String = 'IWantToSolveThisProblem!!'; //you were missing this semicolon
$needle = array('want', 'solve'); //no spaces before the array (not needed, I just like nospaces :P)
foreach($needle as $value) {
$res = stripos($String, $value,0);
if($res !==false){
echo 'found';
}
else {
echo 'not found'; }}
现在这没有意义......这并不是说它只是比较最后一个元素。它只是没有比较第一个。看这个例子...
$haystack = "string to be tested";
$needles = array("string","to","pineapple","tested");
foreach($needles as $needle){
if(stripos($haystack,$needle,0)){
echo "The word \"$needle\" was found in the string \"$haystack\".";
}
else{
echo "The word \"$needle\" was NOT found in the string \"$haystack\".";
}
}
Expected Output:
The word "string" was found in the string "string to be tested".
The word "to" was found in the string "string to be tested".
The word "pineapple" was NOT found in the string "string to be tested".
The word "tested" was found in the string "string to be tested".
Actual Output:
The word "string" was NOT found in the string "string to be tested".
The word "to" was found in the string "string to be tested".
The word "pineapple" was NOT found in the string "string to be tested".
The word "tested" was found in the string "string to be tested".
现在一切都说得通了......来自文档:
“此函数可能返回布尔值 FALSE,但也可能返回非布尔值,其计算结果为 FALSE。有关更多信息,请阅读布尔值部分。使用 === 运算符测试此函数的返回值。”
因此,更改 if(stripos($haystack,$needle,0))
以if(stripos($haystack,$needle,0) !== False)
修复逻辑。
$String = 'IWantToSolveThisProblem!!';
$needle = array('want', 'solve', 42);
foreach($needle as $value) {
if(stripos($String, $value,0) !== FALSE){
echo "found \"$value\" in \"$String \"";
}
else {
echo "$value not found";
}
}