2013-04-20 121 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

相关问题