Search code examples
phphtml-parsingphp-parse-error

PHP Version number issue


I am not sure why the following code is showing me an error message in PHP 5.2 but it is working perfectly in 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++;
        }
    }

Also, I am using simple_html_dom.php (html parser api) in my code to parse a html page. When I run this code on PHP 5.2 it shows me an error message at "//error here" stating "parse error at line number 67"

Thanks


Solution

  • You're having:

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

    Array dereferencing is a PHP 5.4+ feature, and that's the reason why you're getting this error. You'd have to use a temporary variable if you want this code to work on previous versions of PHP:

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

    Refer to PHP manual for more details.