user1973004
user1973004

Reputation: 15

How do I check if a certain set of words is in a string with PHP

So I have this code below but I am wondering how I can tell if '$text' contains the words 'by owner'. How do I do this? I looked around but I can't find anything.

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

Upvotes: 0

Views: 85

Answers (1)

Eduard Luca
Eduard Luca

Reputation: 6612

Something like:

$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";
}

Upvotes: 1

Related Questions