2016-03-31 14 views
-3

Patlatılmış dizide parçalanmış dizeyi bulmak için strpos kullanmak istiyorum. bir dize sınav testi gibi olmak sınav testi i alana sahip olanlar patlamadan ve finalde ve ben böyle bir dizesi vardır: örneğin : i kötü bir listesi var benim dize: örnek dize i Thats patladı ve Bu dizi gibi: 0 => örnek 1 => dize şimdi örnek dizede kullanılan sınama tersi durumda strpos kullanmak istiyorum ve bana bunu döndürün: sınav örnek kelime kullanılan sınama nedeniyle. bu yüzden, örneğin, sınav veya test yaptırıp yaptırmamanızı istiyorum.strpos php işleviyle haystack ve iğne posionunun tersini nasıl kullanabilirim?

böyle kullanılarak, normal strpos biliyorum:

$mystring = 'example string'; 
$mybad = array('exam', 'test'); 
foreach($mybad as $mybad){ 
$pos = strpos($mystring, $mybad); 
if ($pos === false) { 
echo "not found"; 
} else { 
echo "The string '$mybad' was found in the string '$mystring'"; 
echo " and exists at position $pos"; 
} 
} 

ama yukarıdaki açıklama gibi yapıyor istiyorum ben bu kodu yazdım ama gerçek çalışmıyor.

$mystring = 'example string'; 
$mystring = explode(" ", $mystring); 
$mybad = array('exam', 'test'); 
$mybad = implode(" ", $mybad); 
foreach($mystring as $mystring){ 
$pos = strpos($mybad, $mystring); 
if ($pos === false) { 
echo "not found"; 
} else { 
echo "The string '$mybad' was found in the string '$mystring'"; 
echo " and exists at position $pos"; 
} 
} 

Lütfen bana yardım edin ve hangi yöntemi kullanmalıyım?

Herkese teşekkürler.

+0

üzgün, burada sormaya çalışıyoruz bilmiyorlar. – RiggsFolly

cevap

0

Söylemek istediklerimi anladığımdan emin değilim, ama bunun için yararlı bir şey yaratmaya çalıştım. Belki de en azından konuşmak için bir temeldir.

$mystring = 'example string'; 
$mystring_array = explode(" ", $mystring); 
$mybad_array = array('exam', 'test'); 
foreach($mystring_array as $mystring_element) { 
    foreach($mybad_array as $mybad_element) { 
     $pos = strpos($mystring_element, $mybad_element); 
     if($pos === false) { 
      echo "<br>The string '$mybad_element' was not found in the string '$mystring_element'"; 
     } else { 
      echo "<br>The string '$mybad_element' was found in the string '$mystring_element'"; 
      echo " and exists at position $pos"; 
     } 
    } 
} 

çıktısı verir:

The string 'exam' was found in the string 'example' and exists at position 0 
The string 'test' was not found in the string 'example' 
The string 'exam' was not found in the string 'string' 
The string 'test' was not found in the string 'string' 
+0

Yani, bu şekilde değil. Tam tersini istiyorum. Bir samanlık gibi kötü sözler ve kontrol ettiğim bir iğne olarak iplerimi istiyorum. Yani kontrol ederek, yukarıdaki ilk örnekte, kelimenin bir örneğinin sınav olduğunu söyleyin. Bu örnek gibi => exam ple – sara

+0

'örnek' bir 'samanlık' sınav testinde 'iğne bulunamadı'. Bu tamamen yanlış bir yaklaşım. – hellcode

İlgili konular