Thomas Cox
Thomas Cox

Reputation: 309

Foreach loop and String in php

php Code:

foreach ($html->find('table') as $noscript) {echo $data;
        echo $data;
        echo $noscript->innertext . "<br>";
    }

Now i need to compare every string in this with "google" it can be anything like "GOOGLE" or "Google" , here case doesn't matter.

But how do i check $noscript->innertext and echo $data; for "google"

I am unable to think of a way to implement this. Kindly provide me some code example.

Code i tried

foreach ($html->find('table') as $noscript) {
        //if (strstr(strtolower($data)||strtolower($noscript->innertext), "google"))
        if (preg_match('/google/i', $data || $noscript->innertext)) {
            echo "This is Google";
            $real = 1;
        }


        echo $data;
        echo $noscript->innertext . "<br>";
    }

Second attempt:

if (strstr(strtolower($data)||strtolower($noscript->innertext), "google"))

Both of which are not working.

Upvotes: 1

Views: 857

Answers (3)

Konstantin Pereiaslov
Konstantin Pereiaslov

Reputation: 1814

if(stristr($data,'google') || stristr($noscript->innertext,'google'))
{
    echo "This is Google";
}

Upvotes: 4

hakre
hakre

Reputation: 198117

You might be looking for something called regular expression:

§hasGoogle = preg_match('~goo+gle~i', $text);

That are a language of their own to decribe strings. You find them doucmented in the PHP manual for that function above (preg_match) and in the internet. Perhaps it's well to read a book about them as well as this is a language of it's own.

Note that this expression:

$data || $noscript->innertext

will either be FALSE or TRUE in PHP but not what you think (a string). There ain't no shortcut like this in PHP, so you need to write more code.

Upvotes: 2

user1056272
user1056272

Reputation:

foreach ($html->find('table') as $noscript) {
       if(preg_match("/^google/i", $noscript->innertext) || preg_match("/^google/i", $data)) {
          echo $data;
        }
        echo $noscript->innertext . "<br>";
}

Upvotes: 0

Related Questions