$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";
}
}