Imdb\TitleSearchAdvanced::parse_results PHP Method

parse_results() protected method

protected parse_results ( $page )
    protected function parse_results($page)
    {
        $doc = new \DOMDocument();
        @$doc->loadHTML($page);
        $xp = new \DOMXPath($doc);
        $resultSections = $xp->query("//div[@class='article']//div[@class='lister-item mode-advanced']");
        $ret = array();
        foreach ($resultSections as $resultSection) {
            $titleElement = $xp->query(".//h3[@class='lister-item-header']/a", $resultSection)->item(0);
            $title = trim($titleElement->nodeValue);
            preg_match('/tt(\\d{7})/', $titleElement->getAttribute('href'), $match);
            $id = $match[1];
            $ep_id = null;
            $ep_name = null;
            $ep_year = null;
            $yearString = $xp->query(".//span[contains(@class, 'lister-item-year')]", $resultSection)->item(0)->nodeValue;
            if (preg_match('/\\((\\d+)–.+\\)/', $yearString, $match)) {
                $year = $match[1];
                $mtype = 'TV Series';
                $is_serial = true;
                $episodeTitleElement = $xp->query(".//h3[@class='lister-item-header']/a", $resultSection)->item(1);
                if ($episodeTitleElement) {
                    $ep_name = $episodeTitleElement->nodeValue;
                    preg_match('/tt(\\d{7})/', $episodeTitleElement->getAttribute('href'), $match);
                    $ep_id = $match[1];
                    $yearString = $xp->query(".//span[contains(@class, 'lister-item-year')]", $resultSection)->item(1)->nodeValue;
                    if ($yearString) {
                        $ep_year = trim($yearString, '() ');
                    }
                }
            } else {
                preg_match('!\\((\\d+)\\s*(.*?)\\)!', $yearString, $match);
                $year = $match[1];
                $mtype = $match[2] ?: 'Feature Film';
                $is_serial = false;
            }
            $ret[] = array('imdbid' => $id, 'title' => $title, 'year' => $year, 'type' => $mtype, 'serial' => $is_serial, 'episode_imdbid' => $ep_id, 'episode_title' => $ep_name, 'episode_year' => $ep_year);
        }
        return $ret;
    }