1

特定の針の文字列をフィルタリングする方法に関するすべてのスレッドを読みstripos()ましたが、それでうまくいくと思いますが、たとえば最初の針が見つかった場合、私の配列の2番目の値。

例 :

$String = 'IWantToSolveThisProblem!!';
$needle = array('want', 'solve');

foreach ($needle as $value) {

$res = stripos($String, $value,0);
if($res !==false){

echo 'found';
}
else {
echo 'not found'; }}

上記の例では、文字列に両方の値が存在するため、出力に「Found」が表示されます。

問題は、配列の最後の値のみを使用してループしていることです。最初の値が文字列内に存在し、2 番目の値が存在しない場合は、false として返されます。複数の if ステートメントを実行したくない

4

6 に答える 6

0

やったほうがいい:

if(stripos($value,$array[0]) !== false){ //check for the first element 
   echo "found want";
}
if(stripos($value,$array[1]) !== false){ //check for the second element
   echo "found solve";
}
于 2013-04-22T19:46:29.493 に答える
0

私はこのコードでテストしました:

<?php
$str = "IWantToSolveThisProblem!";
$str2 = 'IWantAnAnswer';
$needles = array('want', 'solve');

foreach ($needles as $needle){
        $res = stripos($str2, $needle, 0);
        if ($res !== false){
                echo "$needle found\n";
        }
        else {
                echo "$needle not found\n";
        }
}
echo "\n";
?>

そしてそれは出力します:

$php testme.php

want found
solve not found

ここではうまくいくようです...おそらくタイプミスをチェックするかもしれません..??!

于 2013-04-22T19:48:45.610 に答える
0
$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"; 
    }
}
于 2013-04-22T19:45:25.623 に答える
0
<?php
$String = 'IWantToSolveThisProblem!!';
$needle = array('want', 'solve');

foreach ($needle as $value) {

$res = stripos($String, $value,0);
if($res !==false){

echo 'found';
}
else {
echo 'not found'; }}
?>

出力: 見つかった

foreach ループを if ステートメントに「変換」すると、次のようになります。

if(stripos($String,$needle[0]) !== false){ //check for the first element 
   echo "found";
}
if(stripos($String,$needle[1]) !== false){ //check for the second element
   echo "found";
}

出力: 見つかった

したがって、 foreach - ループはいくつかの if 条件を実行します (配列内の各要素に対して 1 つ)。

于 2013-04-22T21:49:52.570 に答える
0

私はあなたの質問を、指定された干し草の山にすべての針が存在する場合に true を返す関数を見つけるための検索として解釈しました。これがまさにそれを行う関数です...

/**
 * Search the haystack for all of the given needles, returning true only if 
 * they are all present.
 * 
 * @param  string  $haystack  String to search
 * @param  array   $needles   Needles to look for
 * @return boolean            
 */
function stripos_array($haystack, array $needles)
{
    foreach ($needles as $needle)
    {
        if (stripos($haystack, $needle) === false) return false;
    }
    return true;
}

// Test the function

$string = 'IWantToSolveThisProblem!!';

$search = array('want', 'solve');
$found = stripos_array($string, $search);
echo $found ? 'found' : 'not found', "\n"; // found

$search = array('want', 'cheese');
$found = stripos_array($string, $search);
echo $found ? 'found' : 'not found', "\n"; // not found

$search = array('cheese', 'problem');
$found = stripos_array($string, $search);
echo $found ? 'found' : 'not found', "\n"; // not found

すべての針ではなく、いずれかの針が見つかった場合に true を返すようにこの関数を変更するのは簡単です。

/**
 * Search the haystack for any of the given needles, returning true if any of
 * them are present.
 * 
 * @param  string  $haystack  String to search
 * @param  array   $needles   Needles to look for
 * @return boolean            
 */
function stripos_array($haystack, array $needles)
{
    foreach ($needles as $needle)
    {
        if (stripos($haystack, $needle) !== false) return true;
    }
    return false;
}
于 2013-04-22T22:31:47.307 に答える
-1

$value が "solve" になると見つからないため、"not found" を取得しています。$String = 'IWantAnAnswer'.

于 2013-04-22T20:21:43.663 に答える