2016-09-30 20 views
-1

文字列に一致させる単語/句の配列があります。文字列の中にデータベーステーブルに挿入したいのですが、文字列やフレーズ全体と一致する必要があります。それ故一致しPHP文字列の単語の配列と一致する

$string = "This is a Sample sting of information"; 
$words = array('This is', 'Test', 'sample', 'information', 'sting of information'); 

:たとえば をこれは、情報

の サンプル 情報 刺さある。また大文字と小文字を区別しません。私がこれまで持っているが、立ち往生しています

$string = "This is a Sample sting of information"; 
$words = array('This is', 'Test', 'sample', 'information', 'sting of information');   
foreach ($words as $word) { 
     if (strstr($string,$word) !== false) { 
      echo $word." - NO<br>"; 
     } 
     else { 
      echo $word." - YES<br>"; 
     } 
    } 
+0

すべてのタイプミスで判断すると、私はそれほど驚くことではありません:情報> ** informtaion ** – CD001

+0

あなたのコードは大丈夫です、 'if(true){true} else {false } 'あなたの例で' if(strstr($ string、$ word)!== false){ echo $ word。 " - YES
"; } else { echo $ word。 " - NO
"; } ' – devpro

+0

' == false'をがTRUE FALSEない – devpro

答えて

0

は、私が思うこの

foreach ($words as $word) { 
if(stripos($string, $word) !== false) { 
    echo "Yes"."<br>"; 
}else{ 
    echo "no"; 
} 
} 
+0

おかげで、その一致していないサンプルとその大文字と小文字を区別し、私を確認することができますが思う。 – user1315422

+0

@ user1315422 stripertで答えが更新されました。 – Shrikant

+0

は現在OPがどこにあるのかわかりません – devpro

1

を試してみてください、あなたは、これは偽を返しますif (strstr($string,$word) !== false) {考えていますか?

いいえ、キーワードまたはフレーズが見つかった場合はTRUEを返します。

ここに必要なものは?

1 - NOを使用している場合は、StatusをYESに変更するだけで済みます。

2 - YESを使用している場合は、ステータスをNOに変更します。 -

3大文字小文字を区別しない値のためには、stristr()

変形例を使用することができます。

<?php 
$string = "This is a Sample sting of information"; 
$words = array('This is', 'Test', 'sample', 'information', 'sting of informtaion');   

foreach ($words as $word) { 
    var_dump(stristr($string,$word) !== false); // this will help you to understand, what is happening here. 
    if (stristr($string,$word) !== false) { 
     echo $word." - YES<br>"; 
    } 
    else { 
     echo $word." - NO<br>"; 
    } 
} 

?> 

をし、またCD001はコメントで言った@としてあなたがタイプミスを持っている、ということに注意してくださいinformation != informtaion

strstr()は大文字と小文字を区別する関数ですあなたはstristr()にする必要があります。

+0

ありがとうございます、サンプルは見つかりませんでしたが、私はまだ大文字と小文字を区別しています。 – user1315422

+0

@ user1315422ユーザーは、 – devpro

+0

@ user1315422:解決策を確認しましたか? – devpro

関連する問題