Silver007
Silver007

Reputation: 91

How to get IMDb's serie release year and end year?

I've been trying to get out some info from IMDb serie page, including the following data:

It seems like the page include the Release year\End Year in 3 different options:

  1. If the serie is still running: (2018-)
  2. If serie had only 1 year: (2018)
  3. If serie ended after a few years: (2017-2018)

An example html code:

<title>Collateral (TV Mini-Series 2018) - IMDb</title>

My code so far:

         if(isset($_GET['imdb'])) {
                if(isset($_POST['btnGetContent'])) {
                    $cUrl = curl_init(); 
                    curl_setopt($cUrl,CURLOPT_URL, "{$_POST['getContentUrl']}");
                    curl_setopt($cUrl,CURLOPT_RETURNTRANSFER,true);
                    curl_setopt($cUrl,CURLOPT_HEADER, false); 
                    $output = curl_exec($cUrl);
                    curl_close($cUrl);
                    $array = array();
                    if(preg_match('/<h1 itemprop="name"[^>]*>(.*?)\s+g*<\/h1>/',$output,$matches)) {
                        $array["title"] = htmlspecialchars(str_replace("&nbsp;","",$matches[1]));
                    }
                    if(preg_match('/TV Series \((\d{4})(?-:.*)\)/',$output,$matches)) {
                        $array["releaseYear"] = $matches[1];
                    }

Any suggestions would be appreciated.

Upvotes: 0

Views: 108

Answers (1)

Silver007
Silver007

Reputation: 91

                        if(preg_match('/<title>.*?\((.*?)\).*?IMDb<\/title>/',$output,$matches)) {
                        //(YYYY-YYYY)
                        if(preg_match('/.*?(\d{4}).*?(\d{4})/',$matches[1],$match)) {
                            $array["releaseYear"] = $match[1];
                            $array["endYear"] = $match[2];
                        }
                        // (YYYY- )
                        else if(preg_match('/.*?(\d{4})./',$matches[1],$match)) {
                            $array["releaseYear"] = $match[1];
                            $array["endYear"] = "0";
                        }
                        // (YYYY)
                        else if(preg_match('/.*?(\d{4})/',$matches[1],$match)) {
                            $array["releaseYear"] = $match[1];
                            $array["endYear"] = $match[1];
                        }
                    }

Upvotes: 0

Related Questions