1

我不确定为什么以下代码在 PHP 5.2 中向我显示错误消息,但它在 php 5.4 中运行良好

$f_channelList = array();
    $f_channelCounter = 0;
    $f_channel = null;
    foreach ($f_pageContent->find("div.col") as $f_channelSchedule){
        $f_channel = $f_channelSchedule->find("h2.logo")[0];//error here
        if(trim($f_channel->plaintext) != " " && strlen(trim($f_channel->plaintext))>0){
            if($f_channelCounter == 0){
                mkdir($folderName);
            }
            array_push($f_channelList, $f_channel->plaintext);
            $f_fileName = $folderName . "/" . trim($f_channelList[$f_channelCounter]) . ".txt";
            $f_programFile = fopen($f_fileName, "x");
            $f_fileContent = $f_channelSchedule->find("dl")[0]->outertext;
            fwrite($f_programFile, $f_fileContent);
            fclose($f_programFile);
            $f_channelCounter++;
        }
    }

另外,我在我的代码中使用 simple_html_dom.php (html parser api) 来解析 html 页面。当我在 PHP 5.2 上运行此代码时,它会在“//error here”处显示一条错误消息,说明“在第 67 行解析错误”

谢谢

4

2 回答 2

1

你有:

$f_channel = $f_channelSchedule->find("h2.logo")[0]; 
                                                ^^^

数组取消引用是 PHP 5.4+ 的一项功能,这就是您收到此错误的原因。如果您希望此代码适用于以前版本的 PHP,则必须使用临时变量:

$temp = $f_channelSchedule->find("h2.logo");
$f_channel = $temp[0];

有关详细信息,请参阅PHP 手册

于 2013-08-31T09:17:16.960 回答
1

您无法访问像 php 5.2 中那样的函数调用的结果。

根据手册

从 PHP 5.4 开始,可以直接对函数或方法调用的结果进行数组取消引用。以前只能使用临时变量。

于 2013-08-31T09:17:40.317 回答