2013-04-20 94 views
0

所以我有下面的代碼,但我想知道如何判斷'$ text'是否包含'所有者'這些字。我該怎麼做呢?我環顧四周,但我找不到任何東西。如何檢查一組字是否在字符串中與PHP

foreach($anchors as $a) { 
    $i = $i + 1; 
    $text = $a->nodeValue; 
    $href = $a->getAttribute('href'); 
    if ($i > 22 && $i < 64 && ($i % 2) == 0) { 
     //if ($i<80) { 
     echo "<a href =' ".$href." '>".$text."</a><br/>"; 
    } 
    // } 
     //$str = file_get_contents($href); 
//$result = (substr_count(strip_tags($str),"ipod")); 
//echo ($result); 
} 
+0

你只是想檢查是否'$ text' 「由所有者」 有子''?原因http://www.php.net/manual/en/function.strstr.php會這樣做...如果你需要不區分大小寫,請使用http://www.php.net/manual/en/function.stristr .php。和http://www.php.net/manual/en/function.preg-match.php將允許您使用正則表達式來匹配變量間距,確切的單詞等。 – FrankieTheKneeMan 2013-04-20 00:12:59

回答

1

喜歡的東西:

$text = $a->nodeValue; 
if(strpos($text, "by owner") == -1){ // if you want the text to *start* with "by owner", you can replace this with strpos($text, "by owner") != 0 
    echo "Doesn't have by owner"; 
} 
else{ 
    echo "Has by owner"; 
} 
+0

謝謝,但有沒有一種方法可以檢查對於多個子字符串? – user1973004 2013-04-20 01:16:43

+0

據我所知,PHP沒有這個功能。但你可以自己創建一個。有關示例,請參閱http://stackoverflow.com/a/9220624/898423。 – 2013-04-20 01:32:17

相關問題