PHP SimpleHTMLDom scraping problem
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?
You have the order of the actual parameters reversed in your call to stripos. See http://us3.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)) {
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;
}
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
精彩评论