0

I'm trying to do a scrape with SimpleHTMLDom and seem to be running in to a problem.

My code is as follows :

$table = $html->find('table',0);
$theData = array();
foreach(($table->find('tr')) as $row) {

    $rowData = array();
    foreach($row->find('td') as $cell) {

        $rowData[] = $cell->innertext;
    }

    $theData[] = $rowData;
}

function array_find($needle, array $haystack)
{
    foreach ($haystack as $key => $value) {
        if (false !== stripos($needle, $value)) {
            return $key;
        }
    }
    return false;
    }

$searchString = "hospitalist";
$position = array_find($searchString, $theData);
echo ($position);

Which yields the following error:

Warning: stripos() [function.stripos]: needle is not a string or an integer in C:\xampp\htdocs\main.php on line 85

What am I doing wrong?

1
  • 1
    it says needle is not a string and should be Commented Aug 10, 2011 at 21:51

3 Answers 3

1

You have the order of the actual parameters reversed in your call to stripos. See https://www.php.net/manual/en/function.stripos.php. Just reverse the order of the arguments and that error should be fixed.

Change:

if (false !== stripos($needle, $value)) {

to

if (false !== stripos($value, $needle)) {
Sign up to request clarification or add additional context in comments.

Comments

1

From the docs, you should be passing in the needle second, not first. Try this:

function array_find($needle, array $haystack)
{
    foreach ($haystack as $key => $value) {
        if (false !== stripos($value, $needle)) {
            return $key;
        }
    }
    return false;
    }

Comments

0

The message is referring to the function argument of stripos and not your variable named $needle.

int stripos ( string $haystack , string $needle [, int $offset = 0 ] )

It is actually complaining about the needle $value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.