【问题标题】:PHP SimpleHTMLDom scraping problemPHP SimpleHTMLDom 抓取问题
【发布时间】:2023-04-10 22:49:01
【问题描述】:

我正在尝试使用 SimpleHTMLDom 进行抓取,但似乎遇到了问题。

我的代码如下:

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

这会产生以下错误:

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

我做错了什么?

【问题讨论】:

  • 它说针不是字符串,应该是

标签: php screen-scraping simple-html-dom


【解决方案1】:

您在调用 stripos 时将实际参数的顺序颠倒了。见http://us3.php.net/manual/en/function.stripos.php。只需颠倒参数的顺序,该错误就会得到修复。

变化:

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

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

【讨论】:

    【解决方案2】:

    来自the docs,您应该是第二个,而不是第一个。试试这个:

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

    【讨论】:

      【解决方案3】:

      该消息指的是stripos 的函数参数,而不是您名为$needle 的变量。

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

      其实是在抱怨$value

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-07
        • 2015-04-05
        相关资源
        最近更新 更多