2016-04-13 17 views
1
ile sorun yaşıyorsanız

Bu işlev 2 eşleşme ile sonuçlansa da 6 eşleşme ile sonuçlanır. Burada ne yapıyorum emin değilim.strpos()

public function displayPrize() { 
     $testString = "The cow jumped over the moon"; 
     $userString = "The cow"; 

     $magicArray = (explode(" ", $testString)); 

     foreach ($magicArray as $value) { 
      if (strpos(" ", $userString, $value) !== false) { 
       $count++; 
      } 
     } 

     echo $count . ' matches'; 
    } 
+0

$ değer bir dize ... strpos param 3 sayı ... – DragonYen

+0

olmasını bekler Dizi aramasını kullan – devpro

+0

Yapmaya çalıştığınız net değil. Neden 2 maçta sonuçlanması gerektiğini düşünüyorsunuz? Strpos() 'ın yapması gerekenleri ne bekliyorsunuz? – Barmar

cevap

0
if (strpos(" ", $userString, $value) !== false) 

array_intersect() kullanılarak

if (strpos($userString, $value) !== false) 
+0

$ userString "Tiyatro korkusu" ise, hala 2 kibrit olduğunu unutmayın - Tiyatro korkusu orijinal dizede olmamasına rağmen. – DragonYen

0

Alternatif bir yol haline gelmelidir:

$testString = 'The cow jumped over the moon'; 
$userString = 'The cow'; 

$testStringArray = explode(' ', $testString); 
$userStringArray = explode(' ', $userString); 

$result = count(array_intersect($testStringArray, $userStringArray)); // 2 
İlgili konular