我不知道为什么下面的代码在PHP5.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解析器api)来解析html页面。当我在PHP5.2上运行这段代码时,它在"//error“上显示了一条错误消息,这里是” stating “,在第67行”“处解析错误
谢谢
发布于 2013-08-31 09:17:16
你有:
$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-31 09:17:40
您不能访问像php5.2中那样的函数调用结果。
根据手册
从PHP5.4开始,可以直接对函数或方法调用的结果进行数组取消引用。以前只能使用临时变量。
https://stackoverflow.com/questions/18546433
复制相似问题