如何检查PHP中的某个字符串是否在字符串中

时间:2013-04-20 00:09:18

标签: php string variables

所以我在下面有这个代码,但我想知道如何判断'$ text'是否包含'by owner'字样。我该怎么做呢?我环顾四周,但找不到任何东西。

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);
}

1 个答案:

答案 0 :(得分: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";
}